[R-sig-Geo] loop- changing varible names
Roger Bivand
Roger.Bivand at nhh.no
Wed Aug 22 07:29:21 CEST 2012
Please do not send HTML-mail, all postings must be text-only.
Comment inline below:
On Wed, 22 Aug 2012, K Jjw wrote:
> Hi ? I am trying to loop over some data while changing the variable
> names. ? My code right now looks like ?
> months<-c(5,4,8,1,9,7,6,2,12,11,10,3)
> mnth_nme<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
> a<-1 ? for (i in months) { ? had_1990<-raster(prec,months) ?
> had_2040<-raster(tot_40,months) ? had_2090<-raster(tot_90,months) ? #all
> the?month data ?
> paste(mnth_nme[a],"_ppt",sep="")<-stack(had_1990,had_2040,had_2090)###
> this? is where it breaks down. ? a<-a+1 ? } ? Basically I want to end up
> with each month's data stacked and called jan_ppt, feb_ppt.... ? I tried
> to google the solution and people seem to suggest lists but when I tried
> to make a list I got another error and I am not sure what I am doing
> wrong ?> months<-c(5,4,8,1,9,7,6,2,12,11,10,3) >
> mnth_nme<-c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
> > a<-1 > mnth <- list() > for(i in mnth_nme) + { +
> mnth[[paste(mnth_nme[a]"_ppt",sep="")]] Error: unexpected string
> constant in: "{ mnth[[paste(mnth_nme[a]"_ppt"" ?
Your HTML-mail is not easy to read, and HTML misformatting may lead to
confusion. You should read error messages carefully:
Error: unexpected string constant in: "{ mnth[[paste(mnth_nme[a]"_ppt""
seems clear enough, as you (probably) omitted a comma in the paste() call.
When putting things in loops, do simplify and echo to console:
comp_nm <- paste(mnth_nme[a], "ppt", sep="_")
cat(comp_nm, "\n")
mnth[[comp_nm]] <- ...
to see what is going on, removing the in-loop console output when you know
it works. Arguably the _ppt in the variable name is redundant anyway,
isn't it?
Roger
> Any explanation would be very helpful. Thanks
> ?
> KJ
> [[alternative HTML version deleted]]
>
>
--
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo
mailing list