I have a list of data frames in the following fashion:
> Year<- c(1984, 1999, 2002, 2005, 2010)
> Val <- c(2,1,4,5,4)
> df1 <- cbind.data.frame(Year, Val)
> df1
Year Val
1 1984 2
2 1999 1
3 2002 4
4 2005 5
5 2010 4
> Year<- c(1934, 1989, 2012, 2015, 2000)
> Val <- c(1,5,3,2,1)
> df2 <- cbind.data.frame(Year, Val)
> df2
Year Val
1 1934 1
2 1989 5
3 2012 3
4 2015 2
5 2000 1
> Year <- c(1984, 1999, 2002, 2005, 2010)
> Val <- c(5,7,8,3,4.5)
> df3 <- cbind.data.frame(Year, Val)
> df3
Year Val
1 1984 5.0
2 1999 7.0
3 2002 8.0
4 2005 3.0
5 2010 4.5
> c <- list(df1, df2, df3)
> c
[[1]]
Year Val
1 1984 2
2 1999 1
3 2002 4
4 2005 5
5 2010 4
[[2]]
Year Val
1 1934 1
2 1989 5
3 2012 3
4 2015 2
5 2000 1
[[3]]
Year Val
1 1984 5.0
2 1999 7.0
3 2002 8.0
4 2005 3.0
5 2010 4.5
For my assignment I would like to select data frames from the list c
, with the same Year
values. So, that in another list, say d
I have all the data frames from list c
which have the same years. How to approach this problem? Thank you.
CodePudding user response:
We may use intersect
i.e, extract the 'Year' column from the list
, get the intersecting 'Year' values, and then do a subset
to get the rows having only that 'Year' values in each of the list
elements by looping over the list
with lapply
years <- Reduce(intersect, lapply(c, `[[`, 'Year'))
c1 <- lapply(c, subset, subset = Year %in% years)