0

I need to reshape this data below in long format stacking four columns A C G and T columns together. I have found many answers for two columns, but could not find for more than two columns.

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))
Uwe
  • 41,420
  • 11
  • 90
  • 134
MAPK
  • 5,635
  • 4
  • 37
  • 88

2 Answers2

0

Here is my answer:

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

with tidyr:

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
phiver
  • 23,048
  • 14
  • 44
  • 56