Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
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]Example 1
Following snippet creates a sample data frame −
xThe 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 3To replace 0s in df1 with NA, add the following code to the above snippet −
xOutput
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 3To 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 3Example 2
Following snippet creates a sample data frame −
yThe 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 1To replace 0s in df2 with NA, add the following code to the above snippet −
yOutput
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 1To 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
