我如何在R中将四列融化在一起

时间:2018-07-28 15:54:24

标签: r reshape melt

我需要在下面用长格式重整此数据,将四列A C GT列堆叠在一起。我为两栏找到了很多答案,但找不到两栏以上。

mydata<- structure(list(Pos = 1:18, A = c(1.22050447518308, 0.976403580146461, 
0.244100895036615, 0.081366965012205, 0.569568755085435, 0.32546786004882, 
0.244100895036615, 0.32546786004882, 0.48820179007323, 0.732302685109845, 
1.87144019528072, 1.05777054515867, 0.48820179007323, 0.32546786004882, 
0.16273393002441, 0.65093572009764, 0.406834825061025, 18.5516680227827
), C = c(0.813669650122051, 0.65093572009764, 0.48820179007323, 
0.32546786004882, 0.813669650122051, 0.976403580146461, 0.244100895036615, 
2.27827502034174, 0.406834825061025, 0.895036615134255, 0.976403580146461, 
0.244100895036615, 0.244100895036615, 0.244100895036615, 0.569568755085435, 
0.569568755085435, 1.5459723352319, 5.20748576078112), G = c(1.13913751017087, 
1.30187144019528, 0.813669650122051, 0.48820179007323, 1.13913751017087, 
0.895036615134255, 5.93978844589097, 1.30187144019528, 0.569568755085435, 
0.732302685109845, 0.569568755085435, 0.48820179007323, 0.48820179007323, 
0.48820179007323, 0.732302685109845, 0.569568755085435, 1.70870626525631, 
6.10252237591538), T = c(0.569568755085435, 0.16273393002441, 
0.244100895036615, 0.32546786004882, 2.27827502034174, 0, 0.406834825061025, 
0.081366965012205, 0.244100895036615, 0.244100895036615, 0.244100895036615, 
0, 0.081366965012205, 0.16273393002441, 0.244100895036615, 0.48820179007323, 
1.05777054515867, 21.4808787632221)), .Names = c("Pos", "A", 
"C", "G", "T"), class = "data.frame", row.names = c(NA, -18L))

2 个答案:

答案 0 :(得分:0)

这是我的答案:

 reshape(mydata, direction = "long", varying = list(names(mydata)[2:5]), v.names = "Value", 
          idvar = c("Pos"), timevar  = "Values", times = c("A","C","G","T"))

答案 1 :(得分:0)

与提迪尔:

gather(mydata, key = "cols", value = "values", -Pos)
   Pos cols       value
1    1    A  1.22050448
2    2    A  0.97640358
3    3    A  0.24410090
4    4    A  0.08136697
5    5    A  0.56956876
6    6    A  0.32546786