R mutate(缺少“ .data”,没有默认值)

时间:2018-12-24 10:39:42

标签: r mutate

我在R中编写了此函数

match <- followers %>%
  +   mutate(x= ifelse(followers$screen_name %in% xnames$screen_name, 1, 0),y= ifelse(followers$screen_name %in% ynames$screen_name,   1, 0))            

但是我收到此错误,我无法解决

Error in mutate_(.data, .dots = compat_as_lazy_dots(...)) : 
  argument ".data" is missing, with no default

此处提供有关数据的更多信息

  > str(xnames)
    'data.frame':   5000 obs. of  1 variable:
     $ screen_name: chr  "27khT5dIkDpu4hl" "M8ibrahem" "IBRAHEM87711" "ryooon85" ...
    > str(ynames)
    'data.frame':   5000 obs. of  1 variable:
     $ screen_name: chr  "rosebiose" "S7q0QTdIk8SLcNm" "DleGQquoaUGxQzn" "RxafjLONGO6UgS8" ...
str(followers)
'data.frame':   9925 obs. of  1 variable:
 $ screen_name: chr  "27khT5dIkDpu4hl" "M8ibrahem" "IBRAHEM87711" "ryooon85" ...

这是数据样本

    > head(xnames)
          screen_name
    1 27khT5dIkDpu4hl
    2       M8ibrahem
    3    IBRAHEM87711
    4        ryooon85
    5        sqrr1233
    6          3ram16

> head(ynames)
      screen_name
1       rosebiose
2 S7q0QTdIk8SLcNm
3 DleGQquoaUGxQzn
4 RxafjLONGO6UgS8
5 YlirzqF0N9EfeOY
6      AL_3GEEEED

> head(followers)
      screen_name
1 27khT5dIkDpu4hl
2       M8ibrahem
3    IBRAHEM87711
4        ryooon85
5        sqrr1233
6          3ram16

我也尝试将列的类型转换为factor,但仍然给我相同的错误

谢谢

1 个答案:

答案 0 :(得分:1)

这里有一个伪造数据的解决方案,我建议您也使用dplyr::if_else而不是ifelse,这样更安全:

library(dplyr)
followers <- data.frame(screen_name = c(' 27khT5dIkDpu4hl','M8ibrahem','IBRAHEM87711','ryooon85','sqrr1233','3ram16'))
xnames <- data.frame(screen_name = c('27khT5dIkDpu4hl','IBRAHEM87711','ryooon85','sqrr1233','3ram16'))
ynames <- data.frame(screen_name = c('rosebiose','S7q0QTdIk8SLcNm','DleGQquoaUGxQzn','RxafjLONGO6UgS8','YlirzqF0N9EfeOY','AL_3GEEEED'))


followers %>% mutate(x = if_else(screen_name %in% xnames$screen_name,1,0),
                     y = if_else(screen_name %in% ynames$screen_name,1,0))
       screen_name x y
1  27khT5dIkDpu4hl 0 0
2        M8ibrahem 0 0
3     IBRAHEM87711 1 0
4         ryooon85 1 0
5         sqrr1233 1 0
6           3ram16 1 0