R reshape2 'Aggregation function missing: default to length' [duplicate]

时间:2023-02-11 22:49:10

This question already has an answer here:

这个问题已经有了答案:

I have seen this reshape2 several times on SO but haven't seen a solution to my particular problem;

我已经见过好几次了,但是还没有找到解决我的问题的办法;

I have a dataset like this;

我有这样的数据集;

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

I am trying to cast this to a wide format that looks like this;

我试着把它转换成这样的宽格式;

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

using;

使用;

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

but the data ends up looking like something like this;

但数据最终是这样的;

Student    Exam1     Exam2
Adam        0          0
John        0          1

with this error;

这个错误;

Aggregation function missing: defaulting to length

Any ideas why it is changing all of these values to a (0 or 1)?

你知道为什么所有的这些值都变成a(0或1)了吗?

1 个解决方案

#1


11  

Thanks to @akrun who pointed it out.

感谢@akrun指出了这一点。

Well, there's a high chance that your data has duplicate row that look either like this:

好吧,你的数据很有可能有重复的行是这样的:

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

Or like this:

或者像这样:

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

When you cast it like this: dcast(data, student + class ~ test, value.var='score')

当你这样转换时:dcast(data, student + class ~ test, value.var='score)

#1


11  

Thanks to @akrun who pointed it out.

感谢@akrun指出了这一点。

Well, there's a high chance that your data has duplicate row that look either like this:

好吧,你的数据很有可能有重复的行是这样的:

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

Or like this:

或者像这样:

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

When you cast it like this: dcast(data, student + class ~ test, value.var='score')

当你这样转换时:dcast(data, student + class ~ test, value.var='score)