重新整理一列中的值的数据 [英] Reshape data for values in one column

查看:122
本文介绍了重新整理一列中的值的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的data.frame看起来像这样

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

我想得到这样的东西:

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

它与reshape()一起工作到宽格式,只有少数情况,但整个数据frame(约23.000 ID)reshape()需要太长时间。
Melt()和cast()会重新整形数据,但将test_result中的值替换为测试的频率。任何其他想法如何管理?谢谢!

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!

推荐答案

从reshape2包中删除这样做:

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

这篇关于重新整理一列中的值的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆