Skip to content

creating a list of lists from a data frame

2 messages · Srinivas Iyyer, jim holtman

#
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:
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
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
[[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.
[[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:
list(src=as.character(x[1,]$src),title=as.character(x[1,]$title),probes=as.character(x[1,]$probesets))
list(src=as.character(x[2,]$src),title=as.character(x[2,]$title),probes=as.character(x[2,]$probesets))
list(src=as.character(x[3,]$src),title=as.character(x[3,]$title),probes=as.character(x[3,]$probesets))
list(src=as.character(x[4,]$src),title=as.character(x[4,]$title),probes=as.character(x[4,]$probesets))
[[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"
#
Is this what you want?
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
+     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: