[R] Question about converting list items in matrix

David Winsemius dwinsemius at comcast.net
Wed Jul 20 23:36:57 CEST 2011


On Jul 20, 2011, at 5:04 PM, Vickie S wrote:

>
> Hi friends,
> I have got a list where each element might have variable number of  
> members.
> $`4213`
> [1] "214077_x_at"
>
> $`164832`
> [1] "225996_at" "235977_at"
>
> $`339010`
> [1] NA
>
> $`23410`
> [1] "221562_s_at" "221913_at"   "49327_at"
>
> $`285386`
> [1] "229764_at"
>
> $`2099`
> [1] "205225_at"   "211233_x_at" "211234_x_at" "211235_s_at"
> [5] "211627_x_at" "215551_at"   "215552_s_at" "217163_at"
> [9] "217190_x_at"
>

If you had posted the output from dput(head(this_list)) it would have  
made testing easier. Observe:

 > dput(this_list)
structure(list(`4213` = "214077_x_at", `164832` = c("225996_at",
"235977_at"), `339010` = NA, `23410` = c("221562_s_at", "221913_at",
"49327_at"), `285386` = "229764_at"), .Names = c("4213", "164832",
"339010", "23410", "285386"))

So now all one need to do is stick a "<-" in between the name and the  
structure expression and one has a "reproducible example".

(testing on these first four entries)

 >  matrix( c(  rep(names(this_list), sapply(this_list, length)) ,
+              unlist(this_list)
+            ) , ncol=2)
      [,1]     [,2]
[1,] "4213"   "214077_x_at"
[2,] "164832" "225996_at"
[3,] "164832" "235977_at"
[4,] "339010" NA
[5,] "23410"  "221562_s_at"
[6,] "23410"  "221913_at"
[7,] "23410"  "49327_at"
[8,] "285386" "229764_at"


> I want to integrate this list in a matrix format like this :
>
> 4213 "214077_x_at"
> 164832  "225996_at"
> 164832 "235977_at"
>
> 339010 NA
> 23410 "221562_s_at"
> 23410 "221913_at"
> 23410 "49327_at"

You cannot mix modes in matrices. They all need to be "character".

> ....
>
> Any idea how can I make such type of matrix ?
>
>
> Thanks,
> Vickie
>
>
>
> 		 	   		
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list