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

From: Srinivas Iyyer <srini_iyyer_bio_at_yahoo.com>
Date: Thu, 27 Mar 2008 15:48:34 -0700 (PDT)

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

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)

Write a for loop that takes each row and creates a list.
Append this list to a master list.


> 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)){



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] "positional"

[1] "positional"

[1] "positional"

[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

src                    probesets 
"positional" "CMAR,USP10,PSORS8,WT3,SLI1"


             title                src         
           "chr4p"       "positional"


       title src probesets
   "chr1q13" "positional" "SCARNA2"


       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] "positional"

[1] "chr16q"


[1] "positional"

[1] "chr4p"

[1] "ATP5I,MHW1,STGD4"

[1] "positional"

[1] "chr1q13"

[1] "SCARNA2"

[1] "positional"

[1] "chr4q1"

[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. Received on Thu 27 Mar 2008 - 22:53:25 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 28 Mar 2008 - 10:30:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive