简体   繁体   English

创建一个函数,根据两列的元素是否属于 R 中的列表来选择数据框中的行

[英]Create a function that selects rows in a data frame based on if the elements of two column belong to a list in R

I have a dataframe s1我有一个数据框 s1

s1=data.frame(no=c(1,2,3,4,5,6,7),col=c("red","green","blue","yellow","blue","black","white"),car_mod=c("car2","car4","car1","car5","car7","car3","car1"))

  no    col car_mod
1  1    red    car2
2  2  green    car4
3  3   blue    car1
4  4 yellow    car5
5  5   blue    car7
6  6  black    car3
7  7  white    car1

and a list l和一个列表 l

l=list(list(c("green","blue","red"),c("car1","car2","car5")))

[[1]]
[[1]][[1]]
[1] "green" "blue"  "red"  

[[1]][[2]]
[1] "car1" "car2" "car5"

I want to create a function which only selects the rows in which the element in the column "col" and the element in the column "car_mod" are present in the list ( the element in col should be present in l[1][1] while car_mod should be present in l[1][2])我想创建一个函数,它只选择列表中出现“col”列中的元素和“car_mod”列中的元素的行(col 中的元素应该出现在 l[1][1 ] 而 car_mod 应该出现在 l[1][2] 中)

The output should look something like this输出应该是这个样子

s_new=data.frame(no=c(1,3),col=c("red","blue"),car_mod=c("car2","car1"))

  no  col car_mod
1  1  red    car2
2  3 blue    car1

Note, the actual dataframe and list are very large.请注意,实际数据框和列表非常大。 I tried doing something like this我试着做这样的事情

for(i in l[1]){
  for(j in l[2]){ 
    if(i %in% s1$col & j %in% s1$car_mod){
      select()
    }
   
   
  }
  
}

But im not sure how to proceed or if using loops is the best approach due to the size of the dataframe但我不确定如何继续,或者由于数据帧的大小,使用循环是否是最好的方法

You can use subset (or dplyr::filter ):您可以使用subset (或dplyr::filter ):

> subset(s1, col %in% l[[1]][[1]] & car_mod %in% l[[1]][[2]])

  no  col car_mod
1  1  red    car2
3  3 blue    car1

A posible solution with filter:带过滤器的可能解决方案:

s_new <- s1 %>% filter(col %in% l[[1]][1][[1]] & car_mod %in% l[[1]][2][[1]])

s_new s_new

  no  col car_mod
1  1  red    car2
2  3 blue    car1

To get rid of the [[ you can also use pluck() from the purrr package:要摆脱[[你也可以使用purrr包中的 pluck pluck()

library(tidyverse)

s_new <- s1 %>% filter(col %in% pluck(pluck(l, 1), 1) & car_mod %in% pluck(pluck(l, 1), 2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM