简体   繁体   中英

Efficient way to merge a data frame with a list of lists based on a key value

I've got a list of lists, each of which has identical structure. One of the elements in each sub-list is a key value. I'd like to add to each sub-list a new element, which is the row of a data frame containing the matching key value. I think this is basically like 'merge', except with one list.

So my list and data frame look like this

#Define the list of lists and the data frame
l <- list()
l[[1]] <- list(i=1, data=1:5)
l[[2]] <- list(i=2, data=runif(5))
l[[3]] <- list(i=1, data=rnorm(5))
d <- data.frame(i=c(1, 2), val1=c(TRUE, FALSE), val2 = c("a", "b"))

And I'd like to end up with

> l_mod[[1]]
$i
[1] 1

$data
[1] 1 2 3 4 5

$newData
val1  val2
TRUE     a

And so on for each element of l_mod , with $newData being the appropriate row of d . Note that we can assume that each row of d has a unique value for d$i .

My current approach is to write a matching function and call it from lapply

matchingRow <- function(indexValue, df) {
  return(df[df$i==indexValue, -1])
}

l_mod <- lapply(l, function(x) c(x, newData=matchingRow(x$i, d)))

This basically works, but seems overcomplicated. (Also it splits each column of d into a separate element of the new list; I'd rather it place the whole 1-row data frame as a single list element).

Is there a simpler way of doing this?

With no particular seed. Seems fine to just take out the extra function call.:

l_mod2 <- lapply(l, function(x) list(x, newData=d[d$i==x$i, -1]))

    l_mod2[[3]]
[[1]]
[[1]]$i
[1] 1

[[1]]$data
[1] -0.109289881 -0.577508057  1.416671342 -1.130346543 -0.002256123


$newData
  val1 val2
1 TRUE    a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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