问题
I have the following database (in wide form), "st_all", where I have got two variables I wish to reshape ("P" and "PLC"). The id for the subjects is "g_id".
g_id study condition sample PLC1 PLC2 PLC3 PLC4 PLC5 PLC6 PLC7 PLC8 PLC9 PLC10 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10
1 1 1 1 1 20 20 20 50 50 20 30 20 50 50 1 2 2 1 2 2 1 1 1 1
2 2 1 1 1 60 70 50 70 60 60 60 70 60 50 1 2 1 1 2 2 1 1 1 1
3 3 1 1 1 80 50 55 58 70 50 80 80 60 65 1 2 2 1 2 2 1 1 1 1
4 4 1 1 1 89 51 59 62 72 60 86 80 61 54 1 1 2 1 2 2 1 1 1 1
5 5 1 1 1 90 50 60 70 80 50 90 80 60 50 1 1 1 1 2 2 1 1 1 1
6 6 1 1 1 95 50 60 100 95 60 50 60 60 55 1 2 2 1 2 2 1 1 1 1
To do so I ran the following code:
reshape(st_all,
idvar="g_id",
direction="long",
varying=list(c(5:14),c(15:24)),
v.names=c("PLC","P")
)
and I get the following error:
Error in `row.names<-.data.frame`(`*tmp*`, value = paste(d[, idvar], times[1L], :
invalid 'row.names' length
I have searched for an answer to this, but I do not find any.
Thanks in advance.
回答1:
As noted in the comments, you'll have problems with the reshape
function when your data is a tbl
.
Use as.data.frame
first:
reshape(as.data.frame(st_all),
idvar = "g_id",
direction = "long",
varying = list(c(5:14), c(15:24)),
v.names = c("PLC","P"))
来源:https://stackoverflow.com/questions/34910592/reshape-error-invalid-row-names-length