如何在R中使用聚合函数 [英] How to use Aggregate function in R
本文介绍了如何在R中使用聚合函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请问我如何在R中正确使用聚合函数?
我有这样的数据:
can you help me please how to properly use aggregate function in R? I have data like this:
SPORT FLOWS
[1,] "Other" "1"
[2,] "Other" "1"
[3,] "Other" "1"
[4,] "Other" "1"
[5,] "Other2" "1"
[6,] "Other2" "1"
我需要得到这个:
SPORT FLOWS
[1,] "Other" "4"
[2,] "Other2" "2"
我发现,可以使用聚合函数来完成,但是它不起作用。
I found, that it can be done with aggregate function, but it doesn't work..
谢谢大家。.我已标记出对我有用的答案。
Thank you guys.. I have marked answer which worked for me..
推荐答案
aggregate(FLOWS ~ SPORT, dat, function(x) sum(as.numeric(x)))
其中 dat
是矩阵的名称。
在这里,必须使用函数 is.numeric
才能将第二列转换为数字。
Here, the function is.numeric
is necessary to transform the second column into numbers.
这篇关于如何在R中使用聚合函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文