[R] Odp: How to change row value based on previous row value

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Mon Oct 8 17:14:40 CEST 2007


Petr PIKAL wrote:
> Peter Dalgaard <P.Dalgaard at biostat.ku.dk> napsal dne 08.10.2007 16:31:28:
>
>   
>> Petr PIKAL wrote:
>>     
>>> Hi
>>>
>>> r-help-bounces at r-project.org napsal dne 08.10.2007 14:24:13:
>>>
>>>
>>>       
>>>> Hi,
>>>>
>>>> I would like to fix the data in the following data.frame, and I am
>>>> having trouble coding this in R - help please!
>>>>
>>>>
>>>>         
>>>>> x
>>>>>
>>>>>           
>>>>   A    B   x    y     z
>>>> 1 1 10.0 100 1000 10000
>>>> 2 2 19.8 200 2000 20000
>>>> 3 3 20.1 300 3000 30000
>>>> 4 4 20.3 400 4000 40000
>>>> 5 5 30.0 500 5000 50000
>>>>
>>>> Column B is the problem.
>>>>
>>>> The logic I need to apply (this is in pseudo code ... I don't know 
>>>>         
> how
>   
>>>> to do this in R)
>>>>
>>>> for all rows, r
>>>> if x$B[r] <= x$B[r-1] + a 
>>>>    then x$B[r] = x$B[r-1]
>>>>
>>>> i.e. If a=1, then I would end up with the data.frame
>>>>
>>>>
>>>>         
>>>>> x
>>>>>
>>>>>           
>>>>   A    B   x    y     z
>>>> 1 1 10.0 100 1000 10000
>>>> 2 2 19.8 200 2000 20000
>>>> 3 3 19.8 300 3000 30000
>>>> 4 4 19.8 400 4000 40000
>>>> 5 5 30.0 500 5000 50000
>>>>
>>>>         
>>> [...]
>>>
>>>
>>>       
>>>> b.na <- test$B
>>>> b.na[c(F,diff(test$B)<1)] <- NA
>>>> b.na
>>>>
>>>>         
>> It is not clear that this works. What if test$B[4] is 20.9 instead of 
>>     
> 20.3?
>   
>> There are cases that just don't vectorize. Brute force may be the only
>> solution. It may be slow, but it is available.
>>     
>
> That is what i meant with data in chunks. What if test$B looks like
> seq(1, 10, .1).
> I expected, maybe wrongly, that the data looks like
>
> (n1...nn, big step1, m1...mn, big step2, ....)
>
> and that David wants simply change this to NNNNNNN,MMMMM,....
>
> And there are other ifs is B always increasing?
>
>   
>> X <- test$B
>> for (i in seq_along(X[-1]))
>>    if (X[i] - X[i-1] < 1)
>>       X[i] <- X[i-1]
>> test$B <- X
>>     
>
> X[i-1] is X[0] which does not work, you probably meant X[i+1]
>
>   
Drats!

Actually, I meant

seq_along(X)[-1]

i.e. 2:length(X), except if length(X) <= 1

(That's what you get for trying to get things right...)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-help mailing list