I have attach the photo that describe transpose issue from table 1 to table 2. May I seek your solution for this ?
Asked
Active
Viewed 149 times
0
-
Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – zx8754 Nov 21 '16 at 07:06
1 Answers
0
We can do this with melt/dcast
from data.table
. In the devel version, there is rowid
to create a sequence column by group.
library(data.table)#1.9.7+
dcast(melt(setDT(table1), id.var = 'ID', variable.name = "V"),
V + rowid(ID)~ID, value.var = "value")[, ID := NULL][]
If we are using versions 1.9.6 or below,
dcast(melt(setDT(table), id.var = "ID", variable.name = "V")[,
rn := 1:.N, by = ID], V + rn ~ID, value.var = "value")[, ID := NULL][]
data
table1 <- data.frame(ID = rep(c("GroupA", "GroupB"), c(7, 5)),
V1 = c(2, 2, 4, 3, 1,6, 7, 8, 11, 3, 1, 6),
V2 = c(2, 4, 2, 7, 1, 8, 5, 9, 12, 21, 4, 6))

akrun
- 874,273
- 37
- 540
- 662
-
thanks but it shows some error message :Error in eval(expr, envir, enclos) : could not find function "rowid" ? – user7091378 Nov 21 '16 at 06:46
-
@user7091378 It is in the devel version of data.table 1.9.7. Updated with another option from 1.9.6 – akrun Nov 21 '16 at 06:47