R

Replace R data frame column values conditionally

Replace R data frame column values conditionally

Here are multiple examples of how to replace R data frame values conditionally. Sometimes it is a specific value like NA, but sometimes exact columns, where you want to do the replacement.

For example, R data frame airquality that contains NA and other values.

head(airquality)

#  Ozone Solar.R Wind Temp Month Day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5    NA      NA 14.3   56     5   5
#6    28      NA 14.9   66     5   6

Here is how to replace all NA values in the R data frame.

airquality[is.na(airquality)] <- 0

head(airquality)

#  Ozone Solar.R Wind Temp Month Day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5     0       0 14.3   56     5   5
#6    28       0 14.9   66     5   6

The opposite action. Replace all data frame zero values with NA.

airquality[airquality == 0] <- NA

head(airquality)

Replace data frame column values by using column index and condition. In this case, select the first and the range from the fourth to the fifth column and replace NA in them.

airquality[c(1, 4:5)][is.na(airquality[c(1, 4:5)])] <- 0

head(airquality)

#  Ozone Solar.R Wind Temp Month Day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5     0      NA 14.3   56     5   5
#6    28      NA 14.9   66     5   6

Here is another option. Replace data frame values by using column names and conditions.

airquality[c("Ozone", "Wind")][is.na(airquality[c("Ozone", "Wind")])] <- 0

head(airquality)

#  Ozone Solar.R Wind Temp Month Day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5     0      NA 14.3   56     5   5
#6    28      NA 14.9   66     5   6

 

Replace conditionally using column indices or column names and conditions

Here are other examples. Replace R data frame column values conditionally using column indices or column names and conditions from desired columns. As you can see, it is done by using which function. Here is more about that.

airquality[c(1:4)][which(airquality$Day >= 5), ] <- 0

head(airquality)

#  Ozone Solar.R Wind Temp Month Day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5     0       0  0.0    0     5   5
#6     0       0  0.0    0     5   6

Here is an example with column names.

airquality[c("Ozone", "Wind")][which(airquality$Day >= 5), ] <- 0

Replace R data frame column values conditionally by using part of the column name.

airquality[grepl("on", names(airquality))][which(airquality$Day >= 5), ] <- 0

head(airquality)

#  Ozone Solar.R Wind Temp Month Day
#1    41     190  7.4   67     5   1
#2    36     118  8.0   72     5   2
#3    12     149 12.6   74     5   3
#4    18     313 11.5   62     5   4
#5     0      NA 14.3   56     0   5
#6     0      NA 14.9   66     0   6

 

I hope that some of the examples helped you. Here is another post with some tips and tricks that might be helpful while working with RStudio.

If you want to detect which of the columns contains NA values, then check this Datacornering post.




0 comments on “Replace R data frame column values conditionally

Leave a Reply

Your email address will not be published. Required fields are marked *