+ Reply to Thread
Results 1 to 9 of 9

Thread: Complete beginner

  1. #1
    Points: 13, Level: 1
    Level completed: 25%, Points required for next Level: 37

    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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"
    
    
    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]}
    	}
    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?

    Thank you for your help

  2. #2
    RotParaTon
    Points: 47,169, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,199
    Thanks
    212
    Thanked 1,642 Times in 1,403 Posts

    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.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

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

    trinker (09-09-2012)

  4. #3
    Points: 13, Level: 1
    Level completed: 25%, Points required for next Level: 37

    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Complete beginner

    Quote Originally Posted by Dason View Post
    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. #4
    Cookie Scientist
    Points: 6,080, Level: 50
    Level completed: 65%, Points required for next Level: 70
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    814
    Thanks
    18
    Thanked 320 Times in 245 Posts

    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.
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  6. #5
    RotParaTon
    Points: 47,169, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardMaster TaggerFrequent Poster
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,199
    Thanks
    212
    Thanked 1,642 Times in 1,403 Posts

    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
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  7. #6
    Points: 13, Level: 1
    Level completed: 25%, Points required for next Level: 37

    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #7
    Cookie Scientist
    Points: 6,080, Level: 50
    Level completed: 65%, Points required for next Level: 70
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    814
    Thanks
    18
    Thanked 320 Times in 245 Posts

    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).
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  9. #8
    Points: 13, Level: 1
    Level completed: 25%, Points required for next Level: 37

    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Complete beginner

    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 0s You can see that once there is a "i" such that datap(i)!=0, then datapp will always be non zero.

  10. #9
    Cookie Scientist
    Points: 6,080, Level: 50
    Level completed: 65%, Points required for next Level: 70
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    814
    Thanks
    18
    Thanked 320 Times in 245 Posts

    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.
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

+ Reply to Thread

Posting Permissions

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








Advertise on Talk Stats