[R] creating a list of lists from a data frame

jim holtman jholtman at gmail.com
Fri Mar 28 00:49:24 CET 2008


Is this what you want?

> x
    title        src                  probesets
1  chr16q positional CMAR,USP10,PSORS8,WT3,SLI1
2   chr4p positional           ATP5I,MHW1,STGD4
3 chr1q13 positional                    SCARNA2
4  chr4q1 positional                   MORF4LP4
> lapply(seq(nrow(x)), function(.row){
+     list(src=as.character(x[.row,1]),
+          title=as.character(x[.row,2]),
+          probes=as.character(x[.row,3]))
+ })
[[1]]
[[1]]$src
[1] "chr16q"

[[1]]$title
[1] "positional"

[[1]]$probes
[1] "CMAR,USP10,PSORS8,WT3,SLI1"


[[2]]
[[2]]$src
[1] "chr4p"

[[2]]$title
[1] "positional"

[[2]]$probes
[1] "ATP5I,MHW1,STGD4"


[[3]]
[[3]]$src
[1] "chr1q13"

[[3]]$title
[1] "positional"

[[3]]$probes
[1] "SCARNA2"


[[4]]
[[4]]$src
[1] "chr4q1"

[[4]]$title
[1] "positional"

[[4]]$probes
[1] "MORF4LP4"



On Thu, Mar 27, 2008 at 5:48 PM, Srinivas Iyyer
<srini_iyyer_bio at yahoo.com> wrote:
> Dear group,
> I have a dataframe (x). (4 rows, 3 columns for
> example)
>
> I want to create a master list (my_list) from data
> frame.
>
> I want each row of x to be a list.
>
>
> I tried 3 different ways: writing a loop, using apply,
> and going through each row.
> Going through each row works for me. since I have 1000
> rows, I cannot manually create 1000 lists and combine
> them.  I apologize for the long email with all 3
> methods I tried. I request you to help me if I can
> automate last method. thanks srini
>
> proposal:
> x <- data frame
>
> list1 <- list(src=x[1,]$V1,
>              title = x[1,]$V2,
>              pbset = X[1]$V3)
> list2 <- list(src=x[2,]$V1,
>              title = x[2,]$V2,
>              pbset = X[2]$V3)
>
> master_list <- list(list1,list2)
>
>
> Solution:
> Write a for loop that takes each row and creates a
> list.
> Append this list to a master list.
>
>
> Example:
>
> > x
>    title        src                  probesets
> 1  chr16q positional CMAR,USP10,PSORS8,WT3,SLI1
> 2   chr4p positional           ATP5I,MHW1,STGD4
> 3 chr1q13 positional                    SCARNA2
> 4  chr4q1 positional                   MORF4LP4
>
> > mastr_list = list()
>
> > for(i in 1:nrow(x)){
>     y=list(src=as.character(x[i,src),
>     title=as.character(x[i,]$title),
>     probes=as.character(x[i,]$probesets))
>      mastr_list[i]=y
>    }
>
>
> problem:
> Warning messages:
> 1: In test[i] = y :
>  number of items to replace is not a multiple of
> replacement length
> 2: In test[i] = y :
>  number of items to replace is not a multiple of
> replacement length
> 3: In test[i] = y :
>  number of items to replace is not a multiple of
> replacement length
> 4: In test[i] = y :
>  number of items to replace is not a multiple of
> replacement length
>
> > mastr_list
> [[1]]
> [1] "positional"
>
> [[2]]
> [1] "positional"
>
> [[3]]
> [1] "positional"
>
> [[4]]
> [1] "positional"
>
>
>
> How can I make my master list.
>
> I tried also apply function and result is not exactly
> what I desire.
> >master_list <- apply(x, 1, list)
> > master_list
> [[1]]
> [[1]][[1]]
>                       title
> src                    probesets
>                    "chr16q"
> "positional" "CMAR,USP10,PSORS8,WT3,SLI1"
>
>
> [[2]]
> [[2]][[1]]
>             title                src
> probesets
>           "chr4p"       "positional"
> "ATP5I,MHW1,STGD4"
>
>
> [[3]]
> [[3]][[1]]
>       title          src    probesets
>   "chr1q13" "positional"    "SCARNA2"
>
>
> [[4]]
> [[4]][[1]]
>       title          src    probesets
>    "chr4q1" "positional"   "MORF4LP4"
>
>
>
> my final result should look like this:
> > a =
> list(src=as.character(x[1,]$src),title=as.character(x[1,]$title),probes=as.character(x[1,]$probesets))
> > b =
> list(src=as.character(x[2,]$src),title=as.character(x[2,]$title),probes=as.character(x[2,]$probesets))
> > c =
> list(src=as.character(x[3,]$src),title=as.character(x[3,]$title),probes=as.character(x[3,]$probesets))
> > d =
> list(src=as.character(x[4,]$src),title=as.character(x[4,]$title),probes=as.character(x[4,]$probesets))
> > master_list = list(a,b,c,d)
> > master_list
> [[1]]
> [[1]]$src
> [1] "positional"
>
> [[1]]$title
> [1] "chr16q"
>
> [[1]]$probes
> [1] "CMAR,USP10,PSORS8,WT3,SLI1"
>
>
> [[2]]
> [[2]]$src
> [1] "positional"
>
> [[2]]$title
> [1] "chr4p"
>
> [[2]]$probes
> [1] "ATP5I,MHW1,STGD4"
>
>
> [[3]]
> [[3]]$src
> [1] "positional"
>
> [[3]]$title
> [1] "chr1q13"
>
> [[3]]$probes
> [1] "SCARNA2"
>
>
> [[4]]
> [[4]]$src
> [1] "positional"
>
> [[4]]$title
> [1] "chr4q1"
>
> [[4]]$probes
> [1] "MORF4LP4"
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list