[R] Merge disparate lists

Joshua Wiley jwiley.psych at gmail.com
Wed Oct 27 20:11:51 CEST 2010


Hi Jim,

Here is one way:

# How I imagine your data might be stored
l_one <- list(structure(list(key = c(2, 1, 2)), .Names = "key",
row.names = c(NA,
-3L), class = "data.frame"))
l_two <- list(structure(list(ndx = 1:4, descr = c("this", "that", "other",
"finis")), .Names = c("ndx", "descr"), row.names = c(NA, -4L), class =
"data.frame"))

# selecting rows from "ndx" based on "key", then
# select rows from l_two based on that
l_two[[1]][l_two[[1]][l_one[[1]][, "key"], "ndx"], ]

I was kind of trying to guess what format your data is in.  I created
it as data frames (because your data looks like a data frame)
contained in a list (because you said you had two lists).  If your
data is in a simpler format, this should be even easier.  If you want
a specific example for your data, an easy way is to provide us with
the results of: dput(firstlist)  and dput(secondlist)

If they are very long you can just select the first few rows using
head() or the like (e.g., dput(head(yourdata))   ).

Cheers,

Josh


On Wed, Oct 27, 2010 at 10:53 AM, Jim Burke <j.burke at earthlink.net> wrote:
> My two lists look like below
> Need an R code example that
> combines the two.
>
> l_one
> "key"
> 2
> 1
> 2
>
>
> l_two
> "ndx", "descr"
> 1, "this"
> 2, "that"
> 3, "other"
> 4, "finis"
>
> My goal is a new list that looks
> like below.
> ndx descr
> 2   that
> 1   this
> 2   that
>
> Thanks,
> Jim
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list