I've a a list with a number of data set that corresponds to the object generated below:
data("AirPassengers"); data("mtcars")
lstDta <- list(dtaCars1 = mtcars, dtaCars2 = mtcars,
dtaCars3 = mtcars, dtaOtherStuff = AirPassengers)
I would like to merge the cars data sets on row.names
Results
The results should correspond to the data frame:
res <- merge(
x = merge(x = lstDta$dtaCars1, y = lstDta$dtaCars2, by = "row.names"),
y = lstDta$dtaCars3, by.y = "row.names",
by.x = "Row.names")
where the columns are joined using row.names
(ideally, I would drop the Row.names
variable but this doesn't bother me):
> dim(res)
[1] 32 34
Problem
I want to achieve the same results making use of Reduce
, in particular I am interested in:
- Merging the data frames on the
row.names
- Filtering the list. For example, I want to merge the cars data only and ignore the other data set
Additional requirements
Very useful answer suggests defining a function outside reduce, on the lines of the code:
merge.all <- function(x, y) { merge(x, y, all=TRUE, by="Sample") } output <- Reduce(merge.all, DataList)
I would like to avoid defining the function outside the Reduce
syntax.
Attempt
As shown in the attempt below, I would like to cram everything inside the Reduce
:
dtaMrgd <- Reduce(f = function(x,y) {merge(x,y, by = "row.names")},
lapply(lstDta[grepl("Cars", names(lstDta)) == TRUE]))
so the Reduce
does two things:
- Filters the passed list using string according to matching names
- Uses the filtered object to apply merge function with the desired characteristics
Needles to say, the code above fails.
Notes
I'm specifically interested in a solution that would be of format res <- Reduce( ... )
. I'm not interested in creating some additional objects/functions outside the Reduce()
.