1. ## Complete beginner

Hi,

I am a complete beginner in R, and it seems that some basic computations are taking a huge amount of time. I usually used VBA, that is not know for being especially quick....
Here is the simple thing i am trying to do:

Code:
``````library(Matrix)
Filename="C:\\Users\\OTHNAY\\Desktop\\work\\EURO.CSV"

Taille=3221*94
Datap<-Matrix(0,Taille)
Datapp<-Matrix(0,Taille)
Perf<-Matrix(Taille)

j=0
for(i in 1:3321)
{for(ii in 1:94)
{j=j+1
Datap[j]=data[i,ii]
}
}

Datapp[1]=Datap[1]
for(i in 2:Taille)
{if(Datap[i]!=0)
{Datapp[i]=Datap[i]}
else {Datapp[i]=Datapp[i-1]}
}``````
The begining of the code gets executed pretty quickly, but for the last instruction (Datapp[1]=....), it just takes an unlimited amount of time.

Am i doing something wrong? Is there a better way to do that code?

2. ## Re: Complete beginner

Why don't you explain what you're actually trying to do. Looping in R is usually the wrong way to achieve a goal; R is vectorized and using vectorized operations can speed code up substantially compared to for loops.

3. ## The Following User Says Thank You to Dason For This Useful Post:

trinker (09-09-2012)

4. ## Re: Complete beginner

Originally Posted by Dason
Why don't you explain what you're actually trying to do. Looping in R is usually the wrong way to achieve a goal; R is vectorized and using vectorized operations can speed code up substantially compared to for loops.
I have a vector Datap that has value that can be positive or zero, and i want to create a second vector DataPP such that if Datap(i)>0 then DataPP(i)=Datap(i) but if Datap(i)=0 then DataPP(i)=DataPP(i-1).
Thank you

5. ## Re: Complete beginner

Try this:
Code:
``````set.seed(12345)

Datap <- rpois(10,10)
Datap[sample(10, 5)] <- 0
Datap
# [1] 11  0  0  0  0  4  0  7  8 11

DataPP <- Datap
DataPP[DataPP==0] <- DataPP[which(c(1,DataPP[-1])==0)-1]
DataPP
# [1] 11 11  0  0  0  4  4  7  8 11``````
The solution is quite straightforward except for one small part which looks a little weird (the "c(1,DataPP[-1])" part), because we have to do something a little tricky to make it ignore whether or not the first element of Datap is 0. I assume that is the behavior you want because that is what your original code seems to do.

6. ## Re: Complete beginner

Yes I thought of that too but it has a problem. It doesn't propagate values forward when there are more than 1 zero values in a row. My understanding is the OP would want
Code:
``````Datap
# [1] 11  0  0  0  0  4  0  7  8 11
# to turn into
# [1] 11 11 11 11 11 4 4 7 8 11``````

7. ## Re: Complete beginner

Yes, it should propagate. Actually, as soon as Datap has a non zero value, datapp must not have any from then on.

8. ## Re: Complete beginner

That was what my first solution did, but after examining the OP's code I determined that that was not what he/she was after. However, if it turns out that that solution is what he/she wants, then this code should work:
Code:
``````set.seed(12345)

Datap <- rpois(10,10)
Datap[sample(10, 5)] <- 0
Datap
# [1] 11  0  0  0  0  4  0  7  8 11

# DataPP <- Datap
# DataPP[DataPP==0] <- DataPP[which(c(1,DataPP[-1])==0)-1]
# DataPP

runs <- rle(Datap)
runs\$values[-1][runs\$values[-1]==0] <- runs\$values[which(runs\$values[-1]==0)]
DataPP <- inverse.rle(runs)
DataPP
# [1] 11 11 11 11 11  4  4  7  8 11``````
Edit: just saw stallion's comment above. So the bit of code here is what you want (the code you initially wrote in the OP, from what I can tell, does not propagate forward across 0s).

9. ## Re: Complete beginner

Thanks Jake! It really helps.
Datapp[1]=Datap[1]
for(i in 2:Taille)
{if(Datap[i]!=0)
{Datapp[i]=Datap[i]}
else {Datapp[i]=Datapp[i-1]}
}
i still think that it propagate forward across 0s You can see that once there is a "i" such that datap(i)!=0, then datapp will always be non zero.

10. ## Re: Complete beginner

Yes, I guess it does. I guess I am rusty at reasoning through loops. Like Dason mentioned, it is considered best practice in R to avoid them when possible.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts