0

I have a df of gene names and a df of values for those genes. I want to combine the two (label the data frame with the gene names).

I have a 1x23000 df, "genes":

AB1G, A1CF, ..., ZYY1

and a 8000x23000 df, "expression":

1 0 ... 3

0 0 ... 1

2 2 ... 0

I want to combine them into one df:

A1BG A1CF ... ZYY1

1 0 ... 3

0 0 ... 1

2 2 ... 0

When I use rbind, the gene names are maintained but all non-zero values in "expression" turn to N/A.

When I use colnames(expression)=genes or colnames(expression)=paste(genes), the expression numbers are maintained but the gene names are converted to seemingly meaningless numbers.

Upon further investigation, I saw that the structure of "genes" for some reason is "factor" - when I convert with as.character, all genes get reassigned what seems like a random number (AB1G is now 1143, A1CF is now 967, etc).

I think there may be an issue with incompatible formats of the gene and expression dfs. How can I add character column names to a numeric df?

2
  • 1
    May be you need colnames(expression) <- as.character(unlist(genes)) or colnames(expression) <- c(as.matrix(genes)) Commented Oct 23, 2019 at 17:48
  • try to create a reproducible example with basic R code giving us a couple of dataframes that maybe is a 5 row sample of your data. We need something to be able to reproduce on our side in order to help or understand what your issue is. also create an example of your ideal state post fix. Commented Oct 25, 2019 at 15:23

1 Answer 1

0

The issue of appearance of numeric value instead of string is because the columns were factors and when it got coerced to integer storage mode values. Instead, we can convert to character class after unlisting or convert to matrix which automatically convert the class to character and then remove the dim attributes with c (to convert to vector)

 colnames(expression) <- as.character(unlist(genes))

Or

colnames(expression) <- c(as.matrix(genes)) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.