Paste problem when looping variable assignments

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Paste problem when looping variable assignments

William Armstrong
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=""))
}
Reply | Threaded
Open this post in threaded view
|

Re: Paste problem when looping variable assignments

William Armstrong
One thing I should have mentioned before is that "pds_gagehandles" is a character vector that contains the site names that I am interested in.  An example of a site name is "saho".

So with with the paste function, I am trying to get R to assign a variable with the site name followed by "_potwy".  For example, I am trying to create a vector of events/year named "saho_potwy".

Thanks again.

Billy
Reply | Threaded
Open this post in threaded view
|

Re: Paste problem when looping variable assignments

Jonathan Daily
The function you are looking for is ?assign.

assign(paste(pds_gagehandles[[i]], "_pdswy", sep = ""), ...)

For the reverse, in case you are interested, check out ?get.

HTH,
Jon

On Fri, Apr 22, 2011 at 1:49 PM, armstrwa <[hidden email]> wrote:

> One thing I should have mentioned before is that "pds_gagehandles" is a
> character vector that contains the site names that I am interested in.  An
> example of a site name is "saho".
>
> So with with the paste function, I am trying to get R to assign a variable
> with the site name followed by "_potwy".  For example, I am trying to create
> a vector of events/year named "saho_potwy".
>
> Thanks again.
>
> Billy
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Paste-problem-when-looping-variable-assignments-tp3468414p3468429.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
===============================================
Jon Daily
Technician
===============================================
#!/usr/bin/env outside
# It's great, trust me.

______________________________________________
[hidden email] 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.