M.Douda
M.Douda

Reputation: 574

Replacing values in high-frequency data

I've got high frequency data about durations. I've found out that I've got some faulty entries that I cannot discard that have 1800*random number added to them. Now I was stupid enough to try:

for(i in 1:21863924) {while(rr[i]>=1800){rr[i]=rr[i]-1800}}

Which obviously didn't work even though I left it overnight. I was wondering if there is a more elegant way for this,since subsetting the dataset to exclude the faulty entries works in matter of seconds ?

Upvotes: 1

Views: 40

Answers (1)

akrun
akrun

Reputation: 887691

It can be done in a vectorized way. Create a logical vector

i1 <- rr >= 1800

Use that vector to replace the values while assigning the values to the original vector

rr[i1] <- rr[i1] - 1800

A recursive function would be

f1 <- function(x, val) {
      i1 <- x >= val
       x[i1] <- x[i1] - val
      if(sum(x >= val) > 0) f1(x, val)
   }
out <- f1(rr, val = 1800)
sum(out >= 1800)
#[1] 0

data

set.seed(24)
rr <- sample(20000, 100)

Upvotes: 3

Related Questions