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


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
na.locf(df$Rate)

Example 1

Following snippet creates a sample data frame −

x<-sample(0:3,20,replace=TRUE)
df1<-data.frame(x)
df1

The following dataframe is created −

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

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

x<-sample(0:3,20,replace=TRUE)
df1<-data.frame(x)
df1$x[df1$x==0]<-NA
df1

Output

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

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

To load zoo package and replace NAs with previous value, add the following code to the above snippet −

library(zoo)
na.locf(df1$x)

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

Updated on: 11-Nov-2021

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements