-----Original Message-----
From: ba208 at exeter.ac.uk
Sent: Fri, 3 Apr 2009 11:02:31 +0100
To: bearmarketsrule at inbox.com
Subject: Re: [R] Help pasting string as object name
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!