简体   繁体   English

从列表中删除与R中数据框中的列匹配的数据框

[英]Remove dataframes from list that matches a column in a dataframe in R

I have a list of dataframes. 我有一个数据框列表。 I want to remove some of the dataframes that doesnt match entries from a column on a separate dataframe. 我想从单独数据框上的列中删除一些与条目不匹配的数据框。 Sample code is below. 示例代码如下。

my.list <- list(1.1,1.2,1.3,1.4,1.5)
df <- data.frame(ID = c(1.1,1.3,1.5))

I want to remove dataframes from my.list based on whatever IDs I have in df. 我想根据我在df中拥有的ID从my.list中删除数据框。 So in this case output should look like 因此,在这种情况下,输出应类似于

my.list
$`1.1`
...
$`1.3`
...
$`1.5`

The example input is not very clear, I assume you meant list of dataframes with names 1.1, 1.2, etc., see example: 输入的示例不是很清楚,我假设您的意思是名称为1.1、1.2等的数据帧列表,请参见示例:

# list of dataframes example, here we just have 1 to 5,
# in your case this would be 5 dataframes.
my.list <- as.list(1:5)
names(my.list) <- as.character(c(1.1,1.2,1.3,1.4,1.5))
my.list
# $`1.1`
# [1] 1
# 
# $`1.2`
# [1] 2
# 
# $`1.3`
# [1] 3
# 
# $`1.4`
# [1] 4
# 
# $`1.5`
# [1] 5

df <- data.frame(ID = c(1.1,1.3,1.5))

my.list[ as.character(df$ID) ]
# $`1.1`
# [1] 1
# 
# $`1.3`
# [1] 3
# 
# $`1.5`
# [1] 5

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

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