[R] different way for a for loop for several columns?

Nerak nerak.t at hotmail.com
Tue Feb 14 00:20:50 CET 2012

I have a question about repeating something for several columns.  I
calculated a lot of values for different objects. The values are normally
calculated step-wise for every day for each object. With this obtained data
frame, I make further calculations. Here I have to calculate some things and
I have to take into account the years because I want a number for each year.
I do these first for one object. I use a for loop to take into account the
years. Then, I want to do this as well for the other objects and I use again
another loop around it. The script works well, but it takes a lot of time.
Because of that, I want to see if there is another way to do this.
I made a reproducible script that has some characteristics of my script, but
that is simplified a lot. Instead of daily values, the reproducible script
has only 4 datapoints a year and instead if more than 30 years, here are
only 4 years used. As well, instead of more than 100 objects, the example
has only 3 (C,B,F).  Normally I don’t obtain the dataframe test in this way,
it’s just to create a reproducible script. Normally, the script between the
for (y in 1980:1983) { } loop is also much more extensive, “ 
[which(Year$Date== y)]    “ is used several  times an some small loops are

Reproducible script:


for (l in 1:3)
	for (y in 1980:1983)
	test.3<-test.2[which(Year$Date== y)]
test.1<-cbind(test.1, test.4$length)


You can see that it will take a lot of time for more objects and years. A
problem is that the for (y in 1980:1983) { } takes a lot of time because “
[which(Year$Date== y)] ” is used several times and it takes a lot of time to
search through all the rows. And then, all of this has to be repeated
several times for the different objects.
But actually, it are totally the same calculations that have to be made for
all the objects. Only the input data are different. (calculations are made
with the values of a corresponding columns of data frame test). I thought it
could be faster to calculate each step of the inner loop (for (y in
1980:1983) at the same time for each object . So for example: now,
test.2<-ifelse(test[,l]>1,1,0) is first calculated for year 1980 for object
1, than for year 1981 for object 1 and so on for all the years, this is all
repeated for the different object. I’m looking for a way to calculate
test.2<-ifelse(test[,l]>1,1,0) first for all the objects for year 1980, ten
for all the objects for year 1981 and so on. 

Does somebody knows a way to do this? I was thinking about some kind of form
of apply, but it’s not that I have created a function that has to be applied
on a whole column, calculations are done for the different rows…

Many thanks for your help!
Kind regards,

View this message in context: http://r.789695.n4.nabble.com/different-way-for-a-for-loop-for-several-columns-tp4385705p4385705.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list