[R] Paste problem when looping variable assignments
armstrwa
William.Armstrong at noaa.gov
Fri Apr 22 19:45:51 CEST 2011
Hi all,
I am attempting to run through a large set of data and sort events by the
water year (1 Oct - 30 Sep) in which they occurred.
I have no problem doing this for each individual site I am looking at (so my
problem is not with most of the code, just one minor thing), but when I try
to loop it to do the same things for each site, I run into problems.
I think the root of my problem is that I am trying to switch the name of a
variable for each iteration of the loop. I am using a paste function on the
left side of a "<-" statement, which R doesn't seem to like. I am trying to
create a variable for each site I am looking at (with the format
"sitename_pdswy") that contains how many events occurred in a given year,
which I want to use in further calculations. I keep getting the following
error message:
Error in paste(pds_gagehandles[[i]], "_pdswy", sep = "") <- POTWY_cut :
could not find function "paste<-"
Does anyone know how I might be able to do this? It seems like the type of
thing that must have a simple solution that I am just overlooking..
I have posted my entire code below, with the problematic line bolded and
italicized.
Thank you very much for your help.
Billy
full code:
for(i in 1:length(pds_gagehandles)){
dates<-get(paste(pds_gagehandles[i],"_pds_rawd",sep=""))
q<-get(paste(pds_gagehandles[i],"_pds_rawq",sep=""))
yr<-as.numeric(strftime(dates,format='%Y'))
mo<-as.numeric(strftime(dates,format='%m'))
len<-length(yr)
for(n in 1:len){
WY[n]<-if(mo[n]>=10){
yr[n]+1
}else{
yr[n]}
}
partialpeakind<-which(round(q,2)>=round(pds_TD[i],2))
partialpeaksd<-dates[partialpeakind]
partialpeaksq<-q[partialpeakind]
partialpeaksWY<-WY[partialpeakind]
for(q in 1:length(partialpeaksWY)){
wyrs<-seq(min(partialpeaksWY),max(partialpeaksWY),by=1)
POTWY[[q]]<-sum(partialpeaksWY == wyrs[q])
POTWY_cut<-as.numeric(na.omit(POTWY))
paste(pds_gagehandles[[i]],"_pdswy",sep="")<-POTWY_cut}
pds_dropdates<-data.frame(partialpeaksWY,partialpeaksd,partialpeaksq)
write.table(pds_dropdates,file=paste(pds_gagehandles[i],"_dropdates.txt",sep=""))
write.table(data.frame(wyrs,POTWY_cut),file=paste(pds_gagehandles[i],"_POTWY.txt",sep=""))
}
--
View this message in context: http://r.789695.n4.nabble.com/Paste-problem-when-looping-variable-assignments-tp3468414p3468414.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list