【发布时间】:2020-11-16 20:42:53
【问题描述】:
例如,我有这个小标题:
nationality Other
[1,] White 1 ------> I want to add
[2,] Mexican 0
[3,] American 0
[4,] Asian 1 -------> I want to add
[5,] af 1 -------> I want to add
[6,] American 0
我想以某种方式总结 Other 中的值并创建它自己的 tibble:
Other
[1,] 3
我尝试使用 sum(),但它给了我
Error in FUN(X[[i]], ...) :
only defined on a data frame with all numeric variables
除此之外,tally() 给了我这个,它计算列中的行数:
n
1 88
代码如下:
natgroups3 <- ibtclean %>% select(nationality) %>%mutate(Other = ifelse(str_detect(nationality, "af|White|Asian|white|Middle-Eastern"), yes = 1, no = 0)) %>% drop_na()
【问题讨论】:
-
试试
ibtclean %>% filter(nationality %in% c('White', 'af', 'Asian', 'white', 'Middle-Eastern')) %>% summarise(Other = sum(Other))或ibtclean %>% filter(as.logical(Other)) %>% summarise(Other = sum(Other)) -
sum(as.numeric(x)) 怎么样?
-
谢谢!第一个效果很好,不幸的是, sum(as.numeric()) 给了我一个错误