8

Here an example of my dataframe:

df = read.table(text = 'a  b
120 5
120 5
120 5
119 0
118 0
88 3
88 3
87 0  
10 3
10 3
10 3
7 4
6 0
5 0
4 0', header = TRUE)

I need to replace the 0s within col b with each preceding number diverse than 0.

Here my desired output:

 a  b
120 5
120 5
120 5
119 5
118 5
88 3
88 3
87 3  
10 3
10 3
10 3
7 4
6 4
5 4
4 4

Until now I tried:

df$b[df$b == 0] = (df$b == 0) - 1

But it does not work. Thanks

aaaaa
  • 149
  • 2
  • 18
  • 44

3 Answers3

6

na.locf from zoo can help with this:

library(zoo)
#converting zeros to NA so that na.locf can get them
df$b[df$b == 0] <- NA
#using na.locf to replace NA with previous value
df$b <- na.locf(df$b)

Out:

> df
     a b
1  120 5
2  120 5
3  120 5
4  119 5
5  118 5
6   88 3
7   88 3
8   87 3
9   10 3
10  10 3
11  10 3
12   7 4
13   6 4
14   5 4
15   4 4
LyzandeR
  • 37,047
  • 12
  • 77
  • 87
5

Performing this task in a simple condition seems pretty hard, but you could also use a small for loop instead of loading a package.

for (i in which(df$b==0)) {
  df$b[i] = df$b[i-1]
}

Output:

> df
     a b
1  120 5
2  120 5
3  120 5
4  119 5
5  118 5
6   88 3
7   88 3
8   87 3
9   10 3
10  10 3
11  10 3
12   7 4
13   6 4
14   5 4
15   4 4

I assume that this could be slow for large data.frames

sargas
  • 538
  • 1
  • 5
  • 12
  • For such tasks, the `cum*` functions could be worth some tries. E.g. here `df$b[cummax((df$b > 0) * (1:nrow(df)))]` seems to be correct. – alexis_laz May 05 '17 at 12:27
2

Here is a base R method using rle.

# get the run length encoding of variable
temp <- rle(df$b)
# fill in 0s with previous value
temp$values[temp$values == 0] <- temp$values[which(temp$values == 0) -1]
# replace variable
df$b <- inverse.rle(temp)

This returns

df
     a b
1  120 5
2  120 5
3  120 5
4  119 5
5  118 5
6   88 3
7   88 3
8   87 3
9   10 3
10  10 3
11  10 3
12   7 4
13   6 4
14   5 4
15   4 4

Note that the replacement line will throw an error if the first element of the vector is 0. You can fix this by creating a vector that excludes it.

For example

replacers <- which(temp$values == 0)
replacers <- replacers[replacers > 1]
lmo
  • 37,904
  • 9
  • 56
  • 69