我正在使用类似的数据集执行此操作,并且遇到了相同的问题:
example <- mpg %>%
select(manufacturer) %>%
group_by(manufacturer) %>%
count(manufacturer) %>%
unique() %>%
spread(manufacturer, n)
传播后,我需要合并列信息,但是该函数将数字列'n'更改为未知字符列。当我尝试添加列时,出现此错误:
example <- mpg %>%
select(manufacturer) %>%
group_by(manufacturer) %>%
count(manufacturer) %>%
unique() %>%
spread(manufacturer, n) %>%
mutate(combination = 'ford'+'honda')
mutate_impl(.data,点)中的错误:评估错误:二进制运算符的非数字参数。
感谢您的帮助
编辑: 正如评论所说,删除引号可以解决问题。但是,我的实际数据集的变量名带有空格(为了最终报告的美观),因此我需要找到一种方法来纠正此问题,而不仅仅是删除引号。为此,我这样做:
example <- mpg %>%
mutate(manufacturer = ifelse(manufacturer == 'ford' |
manufacturer == 'honda', "combination", manufacturer)) %>%
select(manufacturer) %>%
group_by(manufacturer) %>%
count(manufacturer) %>%
unique() %>%
spread(manufacturer, n)