user1987607
user1987607

Reputation: 2157

R: print line when column value matches value from other file

In R, I have a list of 11 dataframes named list1. Every dataframe has the same structure:

names    col2    col3 
name1   1       10
name2   2       22
name4   3       40

I have another dataframe called table1 looking like this

names    col4    col5
name1    ...     ...
name2    ...     ...
name3    ...     ...

Now I want to take a subset of my original 11 dataframes. For each dataframe I only want to print the lines where there is a match between the values in the 'names' column of the dataframe and the names column in table1. So in this case, my new dataframe should look like this

names    col2    col3
name1    1       10
name2    2       22

All new dataframe should be appended again in a list2. Could I work with lapply and a match function?

mylist2 <- lapply(mylist1, 
    function(...){
    match(...)
    }

Upvotes: 1

Views: 418

Answers (1)

Veerendra Gadekar
Veerendra Gadekar

Reputation: 4472

You can do something like this

required.list =  lapply(list1, function(x) subset(x, names %in% table1$names))

Upvotes: 1

Related Questions