【问题标题】:R reshape2 'Aggregation function missing: defaulting to length' [duplicate]R reshape2'缺少聚合函数:默认为长度'[重复]
【发布时间】:2015-08-08 10:12:02
【问题描述】:

我已经在 SO 上多次看到这个 reshape2,但还没有看到解决我的特定问题的方法;

我有一个这样的数据集;

head(data)
student    test    score
Adam      Exam1     80
Adam      Exam2     90
John      Exam1     70
John      Exam2     60

我正在尝试将其转换为如下所示的宽格式;

Student    Exam1    Exam2 ........ ExamX
Adam         80       90
John         70       60

使用;

dcast(data,student~test,value.var='score')

但数据最终看起来像这样;

Student    Exam1     Exam2
Adam        0          0
John        0          1

出现此错误;

Aggregation function missing: defaulting to length

知道为什么要将所有这些值都更改为(0 或 1)吗?

【问题讨论】:

  • 您需要提供一个序列列。但是,基于示例,它可以通过 dcast(data, student~test, value.var='score') 提供一个包含重复行的示例
  • 这不是错误。这是一个警告,让您知道,由于您没有为 fun.aggregate 提供值(例如,fun.aggregate=mean),它默认返回长度,这是属于该类别组合的行数的计数.我在您的示例数据中没有看到 job_type。你想要dcast(data,student ~ test ,value.var='score')吗?
  • 嗨,我现在有类似的问题,我不知道如何解决这个问题。问题是value.var 输入错误吗?
  • @Bobesh:这是一段时间以前的事了,但仍然:有时一个简单的object <- unique(object) 可以工作,因为问题可能是由相同的重复行引起的。
  • @AlexDeLarge 在这种情况下什么是对象?

标签: r reshape2


【解决方案1】:

感谢@akrun 指出。

嗯,您的数据很可能有如下所示的重复行:

student    test    score
Adam      Exam1     80
Adam      Exam1     85
Adam      Exam2     90
John      Exam1     70
John      Exam2     60

或者像这样:

student   class     test    score
Adam      Biology   Exam1     80
Adam      Theology  Exam1     85
Adam      Theology  Exam2     90
John      Biology   Exam1     70
John      Theology  Exam2     60

当你像这样投射它时:dcast(data, student + class ~ test, value.var='score')

【讨论】:

  • 仍然出现与上述相同的错误。我听从了你的建议。有什么想法吗?
  • 我尝试了 dcast(data, student ~ test, value.var='score'),但它给出了相同的错误和 0-1 矩阵。
  • 有必要检查一下是否没有重复的行,可能是由于 NAs
猜你喜欢
  • 2017-01-31
  • 1970-01-01
  • 2022-10-13
相关资源
最近更新 更多