Nix
Nix

Reputation: 149

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. So in this case output should look like

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

Upvotes: 0

Views: 45

Answers (1)

zx8754
zx8754

Reputation: 56249

The example input is not very clear, I assume you meant list of dataframes with names 1.1, 1.2, etc., see example:

# 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

Upvotes: 1

Related Questions