[R] Help pasting string as object name
baptiste auguie
ba208 at exeter.ac.uk
Fri Apr 3 12:02:31 CEST 2009
Hi,
Is this what you want?
d <- data.frame(density.AL = seq(1, 10),
density.AK = seq(1, 10), # many others...
Date=letters[1:10]) # dummy example
library(reshape)
melt(subset(d, Date == "b"), id="Date")
BTW, I spotted a few awkward things in your code,
st <- c("AL", "AK")
vars <- paste("d$density", st, sep=".") # easier than mapply etc.
more importantly, in the for loop you should not be incrementing i
manually (as in a while loop), it's already taken care of by the for{}
construct.
HTH,
baptiste
On 3 Apr 2009, at 10:40, Rob Denniker wrote:
> I have a data frame containing monthly observations of the 'density'
> of each US state, recorded in variables named "density.AL",
> "density.AK", "density.AZ", and so on for all 50 states. The data
> frame (called d) also contains a variable called "Date" which is
> encoded as a string in the format "Jan-09", "Feb-09", etc.
>
> I also have a vector st.list<-c("AL", "AK", "AZ",...) of length 50.
>
> I would like a new dataframe with st.list in one column and the
> value of "d$density" for that state when d$Date==Feb-09 in another
> column.
>
> How can I do this?
>
> Here is what I have tried:
>
> names <- rep("d$density",length(st.list))
> templist <- as.vector(mapply(paste, names, st.list ,sep="."))
>
> d.2<-data.frame()
> for (i in 1:length(templist)) {
> d.2$density[i] <- subset(parse(file="",templist[i]),d$Date=="Feb-09")
> i<-i+1 } ### hangs!
>
>
> Thanks for any help!
>
> ____________________________________________________________
> GET FREE 5GB EMAIL - Check out spam free email with many cool
> features!
> Visit http://www.inbox.com/email to find out more!
>
> ______________________________________________
> 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.
_____________________________
Baptiste Auguié
School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK
Phone: +44 1392 264187
http://newton.ex.ac.uk/research/emag
More information about the R-help
mailing list