通过排除从列表中删除元素

时间:2017-09-28 11:54:17

标签: r

我的数据框中有一列,其中每一行都是一个列表。是否可以通过排除从列中删除某些值?

我为矢量尝试了以下方法:

yelp_asian_final %>% mutate(categories = ifelse(categories != "Thai" | categories != "Vietnamese", NULL, 
categories))

但是我收到了以下错误:

Error in mutate_impl(.data, dots) : 
  Evaluation error: replacement has length zero.
In addition: Warning message:
In rep(yes, length.out = length(ans)) :
  'x' is NULL so the result will be NULL

前4行的示例:

> dput(head(yelp_asian_final$categories,4)
+ )
list(c("Thai", "Restaurants"), c("Vietnamese", "Restaurants"), 
    c("Indian", "Restaurants"), c("Restaurants", "Japanese", 
    "Sushi Bars"))

1 个答案:

答案 0 :(得分:1)

以下是dplyr

的想法
library(dplyr)

df1 %>% 
 unnest() %>% 
 filter(v2 != 'Thai'& v2 != 'Vietnamese') %>% 
 nest(v2)

给出,

  v1                              data
1  1               Indian, Restaurants
2  5 Restaurants, Japanese, Sushi Bars
3  6                       Restaurants
4  9                       Restaurants