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.
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:
The begining of the code gets executed pretty quickly, but for the last instruction (Datapp[1]=....), it just takes an unlimited amount of time.Code:library(Matrix) Filename="C:\\Users\\OTHNAY\\Desktop\\work\\EURO.CSV" data<-read.table(Filename,sep=";") 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]} }
Am i doing something wrong? Is there a better way to do that code?
Thank you for your help
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.
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
trinker (09-09-2012)
Try this:
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.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
In God we trust. All others must bring data.
~W. Edwards Deming
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
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
Yes, it should propagate. Actually, as soon as Datap has a non zero value, datapp must not have any from then on.
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:
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).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
In God we trust. All others must bring data.
~W. Edwards Deming
Thanks Jake! It really helps.
About my initial code:
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 0sYou can see that once there is a "i" such that datap(i)!=0, then datapp will always be non zero.
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.
In God we trust. All others must bring data.
~W. Edwards Deming
|
|