Re: [R] attributing values to dataframe positions following eval

From: Greg Snow <Greg.Snow_at_imail.org>
Date: Tue, 08 Jul 2008 09:29:07 -0600

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] <- 5

You 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.
>
______________________________________________ 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 Tue 08 Jul 2008 - 15:39: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 Tue 08 Jul 2008 - 16:32:04 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