[R] Reshaping from long to wide with duplicate idvar and timevar
Sebastien Bihorel
sebastien.bihorel at cognigencorp.com
Tue Mar 14 03:44:47 CET 2017
Hi,
I would like to reshape a data.frame from long to wide format. However, the reshape function does not seem to accept data containing rows with duplicate idvar and timevar. Building upon the ?reshape example:
summary(Indometh)
wide <- reshape(Indometh, v.names = "conc", idvar = "Subject",
timevar = "time", direction = "wide")
Indometh2 <- rbind(Indometh, Indometh2[1,])
wide <- reshape(Indometh2, v.names = "conc", idvar = "Subject",
timevar = "time", direction = "wide")
In the 2nd call, reshape drops the duplicate. In a real world, the process I am working on will handle data with unknown number of duplicates like the one I have manually create above.
One "brute force" way to circumvent this would be to pre-process the data, identify the rows with duplicate idvar/timevar combos, and add some suffixes to the idvar variable to make the rows unique. Then I would call reshape, and finally, I would post-process the data to remove the suffixes... This does not seem very elegant.
Is there a more efficient way to go about this?
Thank you
More information about the R-help
mailing list