其他分享
首页 > 其他分享> > R 报错: x invalid 'type' (character) of argument

R 报错: x invalid 'type' (character) of argument

作者:互联网

运行 R 代码时 figa1= figa %>% group_by(ind) %>% summarize(sum = sum(count)) 提示报错信息:

Error: Problem with summarise() column sum.
i sum = sum(count).
x invalid 'type' (character) of argument
i The error occurred in group 1: ind = >=4.

使用命令 class(figa$count)

[1] "character"

发现是因为count列为字符型,而sum函数要求数值型,因此只需要将count转为数值型即可:

figa1= figa %>% mutate(count = as.numeric(as.character(count))) %>% group_by(ind) %>% summarize(total = sum(count))

mutate(count = as.numeric(as.character(count)))指的是把count转为数值型

标签:count,group,figa,sum,character,argument,报错,ind,type
来源: https://www.cnblogs.com/chenwenyan/p/15822648.html