【问题标题】:R reorder dataframe within subjectR在主题内重新排序数据框
【发布时间】:2010-09-20 16:13:32
【问题描述】:

我有一个大型数据集,其中包含跨学科的重复评估。我怎么去:

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  

谢谢,

乔恩

【问题讨论】:

    标签: r reshape reshape2


    【解决方案1】:

    重塑功能(在统计数据中)相当容易做到这一点:

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

    或者只获取 test1 的结果:

    reshape(subset(data, select=-test2), timevar='assessment', idvar='subj', dir='wide')
    

    【讨论】:

      【解决方案2】:

      您可以使用 hadley 提供的出色的 reshape/reshape2 包轻松完成此操作。这是带您了解所需内容的代码

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

      让我知道这是否适合你。

      【讨论】:

      • 我认为在演员表公式中应该是“变量”而不是“测试”,就像这样:df = cast(df, subj ~ variable + Assessment)。 (除非您将 variable_name = "test" 添加到上一行)。
      • 没错。感谢您指出。我已经相应地修改了代码
      猜你喜欢
      • 2020-09-28
      • 2013-07-03
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多