Re: [R] How to get an argument dynamically from a list?

From: jim holtman <jholtman_at_gmail.com>
Date: Sun, 29 Jun 2008 10:06:58 -0400

Is this what you want:

you have to use data.raw[[polyList[2]]] to access the data. (see ?"[[")

> lapply(polyList, function(x) as.character(data.raw[[x]]))
[[1]]
[1] "A/A" "A/A" "A/A" "A/C" "A/C"

[[2]]
[1] "C/C" NA "C/A" "C/C" "C/A"

[[3]]
[1] "15/27" "13/12" "154/35" "15/12" "13/12"

On Sun, Jun 29, 2008 at 3:24 PM, Stephane Bourgeois <sb20_at_sanger.ac.uk> wrote:
> Hi,
>
> I'd like to get an argument (I think it's the right term) dynamically from a list, but cannot manage to do it.
>
> Here is the code I use:
>
> #output given by 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")
>
> #make dataframe with data
>
> data.raw <- data.frame(t(do.call(data.frame, lapply(sampleList, get))))
> names(data.raw) <- polyList
> row.names(data.raw) <- sampleList
>
> I want to get, for example, data.raw$rs124 using polyList.
>
> I tried
>
>> get(paste("data.raw$", polyList[2], sep=""))
> Error in get(paste("data.raw$", polyList[2], sep = "")) :
> variable "data.raw$rs124" was not found
>
> but it's obviously not the right way, data.raw$rs124 not being a variable per se.
>
> Any idea about how I could do that?
>
> Thanks,
>
> 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?

______________________________________________
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 Sun 29 Jun 2008 - 14:10:07 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 Sun 29 Jun 2008 - 17:31:05 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