1

我有一个大型数据集,跨学科重复评估。我如何从:

subj, assessment, test1, test2  
A,    1,          10,    20  
A,    2,          12,    13  
A,    3,          11,    12  
B,    1,          14,    14  
B,    2,          13,    12

至:

subj, test1_1, test1_2, test1_3  
A,    10,      12,      11  
B,    14,      13  

谢谢,

乔恩

4

2 回答 2

2

you can easily accomplish this using the excellent reshape/ reshape2 package by hadley. here is the code to take you to what you need

library(reshape); 
df = melt(df, id = c('subj', 'assessment'));
df = cast(df, subj ~ variable + assessment);

let me know if this works for you.

于 2010-09-20T16:32:27.533 回答
2

reshape 函数(在 stats 中)很容易做到这一点:

reshape(data, timevar='assessment', idvar='subj', dir='wide')

或者只是得到 test1 的结果:

reshape(subset(data, select=-test2), timevar='assessment', idvar='subj', dir='wide')
于 2010-09-20T16:38:40.270 回答