Achal Neupane
Achal Neupane

Reputation: 5719

Replacing NAs in R with condition to zero

I have this dataframe called mydf. I need to replace the NAs in mydf with (zero) '0' if meets this condition: if number, NA; or NA, number is present; then NA has to be 0 else every other NA's remains as is. The result is shown below.

mydf

A     B     C
1,3   1,NA  NA,1
NA,4  0,0   5,NA
NA    NA,NA NA,6

result

   A     B     C
   1,3   1,0  0,1
   0,4  0,0   5,0
   NA   NA,NA 0,6

Upvotes: 0

Views: 73

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

You can use regular expressions for this task.

mydf[] <- lapply(mydf, sub, pattern = "NA(?=,[0-9])|(?<=[0-9],)NA", 
                 replacement = "0", perl = TRUE)

mydf
#     A     B   C
# 1 1,3   1,0 0,1
# 2 0,4   0,0 5,0
# 3  NA NA,NA 0,6

This regex replaces the following matches with 0: NA followed by a comma and a digit, and NA preceded by a digit and a comma.


For this solution, I assume mydf is structured as follows:

mydf <- structure(list(A = c("1,3", "NA,4", "NA"), B = c("1,NA", "0,0", 
"NA,NA"), C = c("NA,1", "5,NA", "NA,6")), .Names = c("A", "B", 
"C"), row.names = c(NA, -3L), class = "data.frame")

Upvotes: 2

Related Questions