convert dataframe R
1
0
Entering edit mode
12 months ago
sata72 • 0

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 • 671 views
ADD COMMENT
2
Entering edit mode
12 months ago
fracarb8 ★ 1.7k

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
...
ADD COMMENT
0
Entering edit mode

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?

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

Thanks

ADD REPLY

Login before adding your answer.

Traffic: 2154 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6