- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to replace zero with first non-zero occurring at the next position in an R data frame column?
- How to fill NA values with previous values in an R data frame column?
- How to replace missing values with median in an R data frame column?
- How to replace a complete column in an R data frame?
- How to randomly replace values in an R data frame column?
- How to replace numbers written in words with numbers in an R data frame column?
- How to replace space between two words with underscore in an R data frame column?
- How to replace words in a dichotomous column for an R data frame with numbers?\n
- How to replace a particular value in R data frame with a new value?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- How to replace NA with 0 and other values to 1 in an R data frame column?
- How to create a column of first non-zero value in each row of an R data frame?
- How to replace NA values with zeros in an R data frame?
- How to replace space in a string value for some elements in a column of an R data frame?
- How to cut a data frame column with minimum value in R?
