This is a test version of Biostars. For the public version, visit https://www.biostars.org.
convert dataframe R

I want to convert this data to two column:

data

      treat      control
1    0.166666667 0.254237288
2    0.555555556 0.366666667
3    0.000000000 0.083333333
4    0.478260870 0.500000000
5    0.695652174 0.333333333
6    0.000000000 0.000000000
7    0.000000000 0.000000000
8    0.000000000 0.000000000
9    0.000000000 0.000000000
10   0.000000000 0.000000000

convert to:

status  value
control 0.254237288
control 0.366666667
control 0.083333333
control 0.5
control 0.333333333
control 0
control 0
control 0
control 0
control 0
 treat  0.166666667
 treat  0.555555556
 treat  0
 treat  0.47826087
 treat  0.695652174
 treat  0
 treat  0
 treat  0
 treat  0
 treat  0
r

1 answer

More a programming question than bioinfo, but using data.table you can

library(data.table)
df <-as.data.frame(matrix(runif(100,0,100),ncol = 2))
names(df) <- c("treat","control")
df$rows <- rownames(df)

# convert to data.table
setDT(df) 
melt(df,id.vars = "rows")

>   row variable      value
1:   1    treat 64.2108446
2:   2    treat 61.3015108
3:   3    treat 13.6022568
4:   4    treat 60.4887839
5:   5    treat 46.2837096
6:   6    treat 83.8612617
7:   7    treat 13.0699825
8:   8    treat 67.2609841
9:   9    treat 53.6246690
...

Thanks. there is this error:

Error in melt.data.table(data, id.vars = rownames(data)) : 
  One or more values in 'id.vars' is invalid.

The data of two column are not the same dimension there are some NA. may due to that?

You need to pass a column, so id.vars = "rows" should work.

Log in to answer this question.