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

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

```Hi,
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
included.

Reproducible script:
Date<-c(1980,1980,1980,1980,1981,1981,1981,1981,1982,1982,1982,1982,1983,1983,1983,1983)
C<-c(0,0,0,0,5,2,0,0,0,15,12,10,6,0,0,0)
B<-c(0,0,0,0,9,6,2,0,0,24,20,16,2,0,0,0)
F<-c(0,0,0,0,6,5,1,0,0,18,16,12,10,5,1,0)

test<-data.frame(C,B,F)
Year<-data.frame(Date)
test.1<-data.frame(c(1980:1983))
test.4<-data.frame(c(1:4))

for (l in 1:3)
{
for (y in 1980:1983)
{
test.2<-ifelse(test[,l]>1,1,0)
test.3<-test.2[which(Year\$Date== y)]
test.4\$length[y-Year[1,]+1]<-length(which(test.3>0))
}
test.1<-cbind(test.1, test.4\$length)
}
names(test.1)<-c("year","C","B","F")

test.1

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,
Nerak

--
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