-3

I am looking to combine two nested lists. Both lists contain the same amount of sublists. Sublists in both lists are associated in pairs with the same index number, i.e. values in list1$'1' are associated with descriptions in list2$'1'. Order of values in sublists of list1 corresponds to order in sublist in list2 of same index number. I would like to combine those two lists into one table with all categories as columns and the sublist index number as rows.

List1: values

$`1`
[1] "1.0"

$`8`
[1] "1.0"

$`13`
[1] "1.0"

$`2`
[1] "1.0"

$`39`
[1] "6.79E-4" "2.26E-4" "0.99" "1.13E-4"

List2: description

$`1`
[1] "TypeA"

$`8`
[1] "TypeA"              

$`13`
[1] "TypeJ"

$`2`
[1] "TypeH"

$`39`
[1] "TypeE" "TypeG" "TypeD" "TypeA"

Desired result:

     No    TypeA  TypeD    TypeE    TypeG  TypeH  TypeJ
      1      1.0      0        0        0      0      0 
      8      1.0      0        0        0      0      0  
     13        0      0        0        0      0    1.0     
      2        0      0        0        0    1.0      0
     39  1.13E-4   0.99  6.79E-4  2.26E-4      0      0      

Thanks for any advice!

user2299015
  • 127
  • 1
  • 1
  • 3
  • 2
    Please provide your data in a more [reproducible format](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (ie use `dput()`) – MrFlick Mar 01 '16 at 20:51

2 Answers2

2

Another option would be using Map, convert to data.frame, rbind the list elements, and use dcast to convert from 'long' to 'wide' (data from @42- post)

library(data.table)
d1 <- rbindlist(Map(data.frame, List1, List2,
   names(List1), MoreArgs=list(stringsAsFactors=FALSE)))
setnames(d1, paste0("V", seq_along(d1)))
dcast(d1, V3~V2, value.var="V1", fill=0)
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Since you are passing two lists "in parallel" using mapply would see a possible choice but I didn't see a clean method. Instead would try with a for-loop to fill in a matrix with named dimensions. This is as yet untested since I find reconstruction of lists from the print output tedious and agree with MrFlick that it is your responsibility:

mtx <- matrix( NA, nrow=length(List1), ncol=length(unique(unlist(List2))), dimnames=list( names(List1), unique(unlist(List2) ) ) )
for (i in seq_along(List1) ){
  mtx[ names(List1)[i] , List2[[i]] ] <-  List1[[i]]  }
#-------
      TypeA TypeJ TypeH    TypeE    TypeG TypeD
1  1.000000     0     0 0.000000 0.000000  0.00
8  1.000000     0     0 0.000000 0.000000  0.00
13 0.000000     1     0 0.000000 0.000000  0.00
2  0.000000     0     1 0.000000 0.000000  0.00
39 0.000113     0     0 0.000679 0.000226  0.99

I eventually got around to looking up the answer to one of my questions on how to convert "scraped" list output to actual R list objects, although now I see that it adds extraneous back-ticks to the list names, so I went in and removed all the backticks from the input.)

> dput(List1)
structure(list(`1` = 1, `8` = 1, `13` = 1, `2` = 1, `39` = c(0.000679, 
0.000226, 0.99, 0.000113)), .Names = c("1", "8", "13", "2", "39"
))
> dput(List2)
structure(list(`1` = "TypeA", `8` = "TypeA", `13` = "TypeJ", 
    `2` = "TypeH", `39` = c("TypeE", "TypeG", "TypeD", "TypeA"
    )), .Names = c("1", "8", "13", "2", "39"))

(Note that I think you constructed your desired answer incorrectly. Also I observe that the as.numeric() applied to the RHS of the assignment did not seem to be needed, somewhat to my puzzlement).

Community
  • 1
  • 1
IRTFM
  • 258,963
  • 21
  • 364
  • 487