[R] creating a dataframe using a list of the variable names

jim holtman jholtman at gmail.com
Sun Jun 29 03:15:08 CEST 2008


Does this do what you want:

> BOB <- c('A/A', 'C/C', '15/27')
> MARY <- c('A/A', NA, '13/12')
> JOHN <- c('A/A', 'C/A', '154/35')
> CLIFF <- c('A/C', 'C/C', '15/12')
> PAM <- c('A/C', 'C/A', '13/12')
> sampleList <- c("BOB", "MARY", "JOHN", "CLIFF", "PAM")
> polyList <- c("rs123", "rs124", "rs555")
> x <- do.call(data.frame, lapply(sampleList, get))
> names(x) <- sampleList
> row.names(x) <- polyList
> x
        BOB  MARY   JOHN CLIFF   PAM
rs123   A/A   A/A    A/A   A/C   A/C
rs124   C/C  <NA>    C/A   C/C   C/A
rs555 15/27 13/12 154/35 15/12 13/12
>


On Sun, Jun 29, 2008 at 3:50 AM, Stephane Bourgeois <sb20 at sanger.ac.uk> wrote:
> Hello,
>
> I'm fairly new to R, and despite spending quite some time exploring, testing, and looking for answers, I still have a couple of questions remaining...
>
> The first one is about creating a dataframe using a list of the variable names .
>
> I get this output file from a database:
>
> BOB <- c('A/A', 'C/C', '15/27')
> MARY <- c('A/A', NA, '13/12')
> JOHN <- c('A/A', 'C/A', '154/35')
> CLIFF <- c('A/C', 'C/C', '15/12')
> PAM <- c('A/C', 'C/A', '13/12')
>
> sampleList <- c("BOB", "MARY", "JOHN", "CLIFF", "PAM")
> polyList <- c("rs123", "rs124", "rs555")
>
> to create a dataframe with the data I use:
>
> data.raw <- data.frame(BOB, MARY, JOHN, CLIFF, PAM, row.names=polyList)
>
> which works fine, but when there are several hundreds samples... well, you guess my problem.
>
> I'd like to create the dataframe using the list of the variables, contained in sampleList - i.e. something like data.frame(function(sampleList), row.names=polyList)
>
> Is that possible, and if yes, how?
>
> Thanks in advance,
>
> Stephane
>
> ______________________________________________
> 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