Stephane Bourgeois
2008-Jul-08 09:40 UTC
[R] attributing values to dataframe positions following eval
Hi everybody, I've been looking around, but can't seem to find the answer. I get a list of names (which vary, so I never know them in advance), and transform them into variables, each of which is a dataframe, using assign: polyList <- c("rs123", "rs124", "rs555", "rs000") numPoly <- length(polyList) for (k in 1:numPoly) { assign(polyList[k], data.frame(matrix(NA, ncol=3, nrow=3))) } polyList <- lapply(polyList, as.name) I can see the resulting dataframe associated to rs123 (for example) using:> eval(polyList[[1]])X1 X2 X3 1 NA NA NA 2 NA NA NA 3 NA NA NA And see the value in row 1, column 1 with:> eval(polyList[[1]])[1,1][1] NA Now, I want to change the values in the dataframe... that's what I can't manage to do:> eval(polyList[[1]])[1,1] <- 5Error in eval(polyList[[1]])[1, 1] <- 5 : could not find function "eval<-" Anybody has an idea on how to do that? Thank you, Stephane PS: yes, I'm aware that using arrays and dataframes instead of going through the creation of variables may be more practical, but the context is much more complex than the presented example (and in that specific case, having these variables is a requirement) -- The Wellcome Trust Sanger Institute is operated by Genome Research Limited, a charity registered in England with number 1021457 and a compa ny registered in England with number 2742969, whose registered office is 2 15 Euston Road, London, NW1 2BE. [[alternative HTML version deleted]]
John Kane
2008-Jul-08 14:12 UTC
[R] attributing values to dataframe positions following eval
Does not just polyList[[1]])[1, 1] <- 5 work? --- On Tue, 7/8/08, Stephane Bourgeois <sb20 at sanger.ac.uk> wrote:> From: Stephane Bourgeois <sb20 at sanger.ac.uk> > Subject: [R] attributing values to dataframe positions following eval > To: r-help at r-project.org > Received: Tuesday, July 8, 2008, 5:40 AM > Hi everybody, > > > > I've been looking around, but can't seem to find > the answer. > > > > I get a list of names (which vary, so I never know them in > advance), and > transform them into variables, each of which is a > dataframe, using > assign: > > > > polyList <- c("rs123", "rs124", > "rs555", "rs000") > > numPoly <- length(polyList) > > > > for (k in 1:numPoly) { > > assign(polyList[k], data.frame(matrix(NA, ncol=3, > nrow=3))) > > } > > > > polyList <- lapply(polyList, as.name) > > > > I can see the resulting dataframe associated to rs123 (for > example) > using: > > > > > eval(polyList[[1]]) > > X1 X2 X3 > > 1 NA NA NA > > 2 NA NA NA > > 3 NA NA NA > > > > And see the value in row 1, column 1 with: > > > > > eval(polyList[[1]])[1,1] > > [1] NA > > > > Now, I want to change the values in the dataframe... > that's what I can't > manage to do: > > > > > eval(polyList[[1]])[1,1] <- 5 > > Error in eval(polyList[[1]])[1, 1] <- 5 : > > could not find function "eval<-" > > > > Anybody has an idea on how to do that? > > > > Thank you, > > > > Stephane > > > > PS: yes, I'm aware that using arrays and dataframes > instead of going > through the creation of variables may be more practical, > but the context > is much more complex than the presented example (and in > that specific > case, having these variables is a requirement) > > > > > > > -- > The Wellcome Trust Sanger Institute is operated by Genome > Research > > Limited, a charity registered in England with number > 1021457 and a > compa > ny registered in England with number 2742969, whose > registered > office is 2 > 15 Euston Road, London, NW1 2BE. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Greg Snow
2008-Jul-08 15:29 UTC
[R] attributing values to dataframe positions following eval
In the long run it will probably be easier/simpler to work in a list (or a new environment) rather than doing everything in the global environment. For example you can do some of what you are trying below with code like:> polyList <- c("rs123", "rs124", "rs555", "rs000") > mylist <- sapply( polyList, function(x) data.frame(matrix(NA,3,3)), USE.NAMES=TRUE, simplify=FALSE)Now you can print out rs123 using:> mylist[['rs123']]Or> mylist[[ polyList[1] ]]Or> mylist[[1]]Looking at the first row, first column of the first element is> mylist[[1]][1,1]And changing it is just:> mylist[[1]][1,1] <- 5You can also use the elements of mylist by name using the with and within functions:> with(mylist, rs124)> mylist <- within(mylist, rs123[1,1] <- 5)Changing or extracting from all the data frames becomes much easier using lapply or sapply and you don't need to mess around with the eval and assign functions. You also have all your data in a single object to make it easier to save/load/delete/rename/etc. and you are much less likely to accidentally overwrite a variable of interest. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Stephane Bourgeois > Sent: Tuesday, July 08, 2008 3:41 AM > To: r-help at r-project.org > Subject: [R] attributing values to dataframe positions following eval > > Hi everybody, > > > > I've been looking around, but can't seem to find the answer. > > > > I get a list of names (which vary, so I never know them in > advance), and transform them into variables, each of which is > a dataframe, using > assign: > > > > polyList <- c("rs123", "rs124", "rs555", "rs000") > > numPoly <- length(polyList) > > > > for (k in 1:numPoly) { > > assign(polyList[k], data.frame(matrix(NA, ncol=3, nrow=3))) > > } > > > > polyList <- lapply(polyList, as.name) > > > > I can see the resulting dataframe associated to rs123 (for example) > using: > > > > > eval(polyList[[1]]) > > X1 X2 X3 > > 1 NA NA NA > > 2 NA NA NA > > 3 NA NA NA > > > > And see the value in row 1, column 1 with: > > > > > eval(polyList[[1]])[1,1] > > [1] NA > > > > Now, I want to change the values in the dataframe... that's > what I can't manage to do: > > > > > eval(polyList[[1]])[1,1] <- 5 > > Error in eval(polyList[[1]])[1, 1] <- 5 : > > could not find function "eval<-" > > > > Anybody has an idea on how to do that? > > > > Thank you, > > > > Stephane > > > > PS: yes, I'm aware that using arrays and dataframes instead > of going through the creation of variables may be more > practical, but the context is much more complex than the > presented example (and in that specific case, having these > variables is a requirement) > > > > > > > -- > The Wellcome Trust Sanger Institute is operated by Genome Research > > Limited, a charity registered in England with number 1021457 > and a compa ny registered in England with number 2742969, > whose registered office is 2 > 15 Euston Road, London, NW1 2BE. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Possibly Parallel Threads
- How to get an argument dynamically from a list?
- creating a dataframe using a list of the variable names
- Combining 2 columns into 1 column many times in a very large dataset
- maptools, write.polylistShape
- read.table : how to condition on error while opening file?