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转为数值型

上一篇:Oracle索引的作用和用法


下一篇:堆,优先队列,堆排序