r - Replace value using previous row depending on condition (using a function such as sapply) -


i've got large dataset i'm trying find way efficiently.

going through rows in given column want take particular condition , if triggers want replace current element value in element above

for code condition dependent on element being == 2

     [,1] [,2] [1,]    1    1 [2,]    1   32 [3,]    2 4351 [4,]    2    1 [5,]    3    4 [6,]    4    5 [7,]    5 6546 [8,]   67  456 

should become

     [,1] [,2] [1,]    1    1 [2,]    1   32 [3,]    1 4351 [4,]    1    1 [5,]    3    4 [6,]    4    5 [7,]    5 6546 [8,]   67  456 

but @ moment becomes (note changes values simultaneously using sapply, having 2 consecutive 2s make copy 2 above)

     [,1] [,2] [1,]    1    1 [2,]    1   32 [3,]    1 4351 [4,]    2    1 [5,]    3    4 [6,]    4    5 [7,]    5 6546 [8,]   67  456 

this current code same made example:

rowid = 1  letable = cbind(c(1,3,4,5,67,2,2,1),c(1,4,5,6546,456,4351,1,32))  sortedtable =letable[order(letable[,1]),]   print(sortedtable)  abovefunction <- function(x){   print(paste("this x",x))   if(x==2){      print(x);     value=sortedtable[rowid-1,1];     print(paste("if ",x));     rowid <<- rowid+1;     print(rowid)   }    else{     print(x);     value = sortedtable[rowid,1];     print(paste("else ",x));     rowid <<- rowid+1;      print(rowid)   }   return(value) }  sortedcolumn = sapply(sortedtable[,1], abovefunction) print(sortedcolumn) 

is there way can function/replacement in sequence top bottom without resorting loops slow process on large dataset?

use na.locf in zoo package:

library(zoo) na.locf(replace(m, m == 2, na)) 

giving:

     [,1] [,2] [1,]    1    1 [2,]    1   32 [3,]    1 4351 [4,]    1    1 [5,]    3    4 [6,]    4    5 [7,]    5 6546 [8,]   67  456 

note: data used:

m <- structure(c(1l, 1l, 2l, 2l, 3l, 4l, 5l, 67l, 1l, 32l, 4351l,  1l, 4l, 5l, 6546l, 456l), .dim = c(8l, 2l), .dimnames = list(     null, null)) 

update revised use m shown.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -