# How to replace zero with previous value in an R data frame column?

R ProgrammingServer Side ProgrammingProgramming

To replace zero with previous value in an R data frame column, we can use na.locf function of zoo package but to apply this we first need to replace the zero values with NA.

For example, if we have a data frame called df that contains a column say Rate then we can use the below commands to replace 0 values in Rate with previous value by using the below given commands −

df$Rate[df$Rate==0]<-NA

## Output

If you execute all the above given snippets as a single program, it generates the following output: −

[1] 1 2 1 1 2 2 2 3 2 3 3 2 3 3 1 3 2 1 1 3

## Example 2

Following snippet creates a sample data frame −

y<-sample(0:2,20,replace=TRUE)
df2<-data.frame(y)
df2

The following dataframe is created −

   y
1  2
2  0
3  1
4  2
5  1
6  2
7  1
8  1
9  2
10 2
11 2
12 1
13 1
14 2
15 2
16 1
17 2
18 1
19 0
20 1

To replace 0s in df2 with NA, add the following code to the above snippet −

y<-sample(0:2,20,replace=TRUE)
df2<-data.frame(y)
df2$y[df2$y==0]<-NA
df2

## Output

If you execute all the above given snippets as a single program, it generates the following output: −

    y
1   2
2  NA
3   1
4   2
5   1
6   2
7   1
8   1
9   2
10  2
11  2
12  1
13  1
14  2
15  2
16  1
17  2
18  1
19 NA
20  1

To replace NAs with previous value, add the following code to the above snippet −

na.locf(df2\$y)


## Output

If you execute all the above given snippets as a single program, it generates the following output: −

[1] 2 2 1 2 1 2 1 1 2 2 2 1 1 2 2 1 2 1 1 1
Published on 11-Nov-2021 05:05:34