Stephane Bourgeois
2008-Jun-29 12:24 UTC
[R] How to get an argument dynamically from a list?
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
x <- as.matrix(data.raw) x[,2] is this what you want? BOB MARY JOHN CLIFF PAM "C/C" NA "C/A" "C/C" "C/A" On Sun, Jun 29, 2008 at 3:24 PM, Stephane Bourgeois <sb20@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@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. > >-- Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis [[alternative HTML version deleted]]
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?