获取“参数的无效'类型'(字符)”。聚合错误 [英] Getting "invalid 'type' (character) of argument" error with aggregate()
本文介绍了获取“参数的无效'类型'(字符)”。聚合错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下CSV:
color,val2,val3
blue,1,4
green,7,3
blue,4,2
red,9,3
red,2,6
blue,1,7
我只是想按颜色汇总。
在尝试时:
csv <- read.csv("/home/user/file.csv", stringsAsFactors=FALSE)
data <-aggregate(csv, list(csv[["color"]]), sum)
我得到
FUN(X [[i]],...)中的错误:无效的类型(字符)参数
Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument
推荐答案
该错误来自 sum()
,因为您试图对颜色
列中的字符元素求和。
That error is coming from sum()
, because you are attempting to sum the character elements in the color
column.
sum("a")
# Error in sum("a") : invalid 'type' (character) of argument
您需要删除颜色
x 参数中的>列,因为它未在聚合中使用,但实际上是 by
参数。 / p>
You need to remove the color
column from the x
argument, since it is not being used in aggregation, but is actually the by
argument.
aggregate(csv[-1], csv["color"], sum)
# color val2 val3
# 1 blue 6 13
# 2 green 7 3
# 3 red 11 9
但是公式方法也可以使用,并且更干净(但更慢)。
But the formula method would also work and is cleaner (but slower).
aggregate(. ~ color, csv, sum)
这篇关于获取“参数的无效'类型'(字符)”。聚合错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文