Reshape data for values in one column

江枫思渺然 提交于 2019-11-27 07:20:37

问题


My data.frame looks like this

ID | test | test_result
1  |  B   |   10
2  |  A   |   9
3  |  A   |   11
4  |  C   |   7
5  |  F   |   5

And I want to get something like this:

test | test_reult_ID1 | test_result_ID2 | test_result_ID3 ...
 A   |   NA           |     9           |   11
 B   |   10           |     NA          |   NA

It works with reshape() to the wide format with only a few cases but with the whole data frame (about 23.000 ID´s) reshape () takes too long. Melt() and cast() do reshape the data but replace the values in test_result by the frequency of the test. Any other ideas how to manage this? Thanks!


回答1:


dcast from the reshape2 package does this:

require(reshape2)
dcast(data, test ~ ID , value_var = 'test_result' )

#  test  1  2  3  4  5
#1    A NA  9 11 NA NA
#2    B 10 NA NA NA NA
#3    C NA NA NA  7 NA
#4    F NA NA NA NA  5



回答2:


Another solution using reshape function in base R.

reshape(mydf, direction = 'wide', idvar = 'test', timevar = 'ID', 
  v.names = 'test_result', sep = "_")

EDIT. I see that you have already tried reshape and it took too long. Can you provide more details on your actual data?



来源:https://stackoverflow.com/questions/8093839/reshape-data-for-values-in-one-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!