plyr

Reshape three column data frame to matrix (“long” to “wide” format) [duplicate]

孤人 提交于 2019-11-25 22:16:36
问题 This question already has answers here : How to reshape data from long to wide format (11 answers) Closed 28 days ago . I have a data.frame that looks like this. x a 1 x b 2 x c 3 y a 3 y b 3 y c 2 I want this in matrix form so I can feed it to heatmap to make a plot. The result should look something like: a b c x 1 2 3 y 3 3 2 I have tried cast from the reshape package and I have tried writing a manual function to do this but I do not seem to be able to get it right. 回答1: There are many ways

Faster ways to calculate frequencies and cast from long to wide

时间秒杀一切 提交于 2019-11-25 22:15:47
问题 I am trying to obtain counts of each combination of levels of two variables, \"week\" and \"id\". I\'d like the result to have \"id\" as rows, and \"week\" as columns, and the counts as the values. Example of what I\'ve tried so far (tried a bunch of other things, including adding a dummy variable = 1 and then fun.aggregate = sum over that): library(plyr) ddply(data, .(id), dcast, id ~ week, value_var = \"id\", fun.aggregate = length, fill = 0, .parallel = TRUE) However, I must be doing