[R] Manual sort in a for loop

Simon Pickett simon.pickett at bto.org
Thu Mar 26 11:50:56 CET 2009


I would suggest avoiding the function ?assign inside a loop. I used top use 
this until someone here kindly pointed out that it was much easier to catch 
the data of interest in a list...

eg.

df.list <- vector("list", length(10))
for (i in 1:10)}
df.list[[i]]<-data.frame(arunoff_,table_year,_temp)[c(10,7,9,5,4,12,1,3,2,8,11,6),]
}

HTH

Simon.




----- Original Message ----- 
From: "Steve Murray" <smurray444 at hotmail.com>
To: <r-help at r-project.org>
Sent: Wednesday, March 25, 2009 5:58 PM
Subject: [R] Manual sort in a for loop


>
> Dear all,
>
> I am trying to manually re-sort rows in a number of tables. The rows 
> aren't sorted on any particular values but are simply ordered by user 
> choice (as shown by the row numbers in the code). I have been able to 
> carry out each re-arrangement without the use of the 'for' loop, but 
> cannot seem to successfully execute the statements when incorporated into 
> the loop. The code I have is as follows:
>
> table_year=1951
> for (i in (paste("arunoff_",year,"_temp",sep=""))) {
>          assign(paste("arunoff_",table_year, 
> sep=""),paste("arunoff_",table_year,"_temp")[c(10,7,9,5,4,12,1,3,2,8,11,6),])
>          table_year = table_year+1
>          }
>
>
> The error I get is:
>
> Error in paste("arunoff_", table_year, "_temp")[c(10, 7, 9, 5, 4, 12,  :
>  incorrect number of dimensions
>
> ...depsite this not occurring when I do each table individually (so it 
> can't be a case of there not being enough rows, as> dim(arunoff_1951_temp) 
> gives [1] 12 11
>
> I have a feeling that it may be a syntax error, possibly between 'temp' 
> and the square bracket, but I can't be sure of this.
>
>
> Any solutions or advice offered would be gratefully received.
>
> Many thanks,
>
> Steve
>
> _________________________________________________________________
> [[elided Hotmail spam]]
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list