【发布时间】:2020-09-25 14:18:49
【问题描述】:
我想使用循环过滤数据框的多个列,删除任何给定列值在特定列表中的行。
例如:
> my_df <- data.frame(word1 = c("one", "two", "red", "blue"), word2 = c("apple","orange","banana","pear"), word3 = c("red", "orange", "yellow", "green"))
> color_words = c("red", "orange", "yellow", "green", "blue")
> my_df
word1 word2 word3
1 one apple red
2 two orange orange
3 red banana yellow
4 blue pear green
使用 dplyr filter() 函数:
> my_df %>% filter(!word1 %in% color_words) %>% filter(!word2 %in% color_words)
word1 word2 word3
1 one apple red
我第一次尝试循环执行此过滤是:
col_names <- c("word1","word2")
for(col in col_names){
my_df <- my_df %>% filter(!col %in% color_words)
}
> my_df
word1 word2 word3
1 one apple red
2 two orange orange
3 red banana yellow
4 blue pear green
我在使用filter()时读到了quoting and unquoting,所以我也尝试了:
for(col in col_names){
col <- enquo(col)
my_df <- my_df %>% filter(!UQ(col) %in% color_words)
}
> my_df
word1 word2 word3
1 one apple red
2 two orange orange
3 red banana yellow
4 blue pear green
和
for(col in col_names){
my_df <- my_df %>% filter(!UQ(col) %in% color_words)
}
> my_df
word1 word2 word3
1 one apple red
2 two orange orange
3 red banana yellow
4 blue pear green
通过循环进行此过滤的正确方法是什么?
【问题讨论】: