R replace values with NA based on condition

You have tried writing:

R replace values with NA based on condition
r_q:

data <- data %>% if_else(STATUS == "Closed", Address == NA, Address == Address`)

I'll start by just explaining why this didn't work!

  • A pipe puts the left hand side into the first argument of the function on the right hand side. if_else() doesn't take a dataframe as an argument.
  • Address == NA literally means "is the adress equal to NA", which is, a), not what you want and, b), never appropriate anyway as to detect NA values you'd use is.na().

The correct syntax looks like this:

library(tidyverse)

df = read_table("REGION MANAGER STATUS  Address
           Igor Closed  Smith St
           Helena   Open    Peters St
           Igor Closed  Elver St
           Helena   Open    Brownburg St
           Igor Open    Edel St")

df %>%
  mutate(Address = if_else(MANAGER == "Closed", NA_character_, Address))
#> # A tibble: 5 x 4
#>   REGION MANAGER STATUS    Address
#>   <chr>  <chr>   <chr>     <chr>  
#> 1 Igor   Closed  Smith     <NA>   
#> 2 Helena Open    Peters    St     
#> 3 Igor   Closed  Elver     <NA>   
#> 4 Helena Open    Brownburg St     
#> 5 Igor   Open    Edel      St
  • We are changing columns, so we use mutate().
  • The "Address" column is equal to the if_else() call.
  • Our condition is "is the MANAGER column equal to 'closed'?"
  • If TRUE, replace with NA_character_ (as it is a character column)
  • If FALSE, replace with itself (i.e., don't change it)
na_if(1:5, 5:1) #> [1] 1 2 NA 4 5 x <- c(1, -1, 0, 10) 100 / x #> [1] 100 -100 Inf 10 100 / na_if(x, 0) #> [1] 100 -100 NA 10 y <- c("abc", "def", "", "ghi") na_if(y, "") #> [1] "abc" "def" NA "ghi" # na_if() is particularly useful inside mutate(), # and is meant for use with vectors rather than entire data frames starwars %>% select(name, eye_color) %>% mutate(eye_color = na_if(eye_color, "unknown")) #> # A tibble: 87 × 2 #> name eye_color #>#> 1 Luke Skywalker blue #> 2 C-3PO yellow #> 3 R2-D2 red #> 4 Darth Vader yellow #> 5 Leia Organa brown #> 6 Owen Lars blue #> 7 Beru Whitesun lars blue #> 8 R5-D4 red #> 9 Biggs Darklighter brown #> 10 Obi-Wan Kenobi blue-gray #> # … with 77 more rows # na_if() can also be used with mutate() and across() # to mutate multiple columns starwars %>% mutate(across(where(is.character), ~na_if(., "unknown"))) #> # A tibble: 87 × 14 #> name height mass hair_…¹ skin_…² eye_c…³ birth…⁴ sex gender homew…⁵ #>#> 1 Luke… 172 77 blond fair blue 19 male mascu… Tatooi… #> 2 C-3PO 167 75 NA gold yellow 112 none mascu… Tatooi… #> 3 R2-D2 96 32 NA white,… red 33 none mascu… Naboo #> 4 Dart… 202 136 none white yellow 41.9 male mascu… Tatooi… #> 5 Leia… 150 49 brown light brown 19 fema… femin… Aldera… #> 6 Owen… 178 120 brown,… light blue 52 male mascu… Tatooi… #> 7 Beru… 165 75 brown light blue 47 fema… femin… Tatooi… #> 8 R5-D4 97 32 NA white,… red NA none mascu… Tatooi… #> 9 Bigg… 183 84 black light brown 24 male mascu… Tatooi… #> 10 Obi-… 182 77 auburn… fair blue-g… 57 male mascu… Stewjon #> # … with 77 more rows, 4 more variables: species, films, #> # vehicles, starships, and abbreviated variable names #> # ¹​hair_color, ²​skin_color, ³​eye_color, ⁴​birth_year, ⁵​homeworld
dat_ms <- tibble::tribble(~x, ~y, ~z, 1, "A", -100, 3, "N/A", -99, , , -98, -99, "E", -101, -98, "F", -1) dat_ms ( = dat_ms, .predicate = , = ~.x == "N/A") ( = dat_ms, .predicate = , = ~.x %in% ) (dat_ms, to_na = (x = (-99, -98), y = ("N/A"), z = (-101)))

I have a dataset with lots of numerical variables, and a character variables that says whether or not low values are suppressed for that observation. In observations where values aren't suppressed, I want to replace NAs 0s (just for specific variables), and I can't figure it out. This is my data:

suppressed var1 var2
      none    2    6
      none   NA    6
      none    3    7
      none   NA   NA
      full    2    6
      full    3    6
      none    3   NA
      partial NA    6
      none    2    7
      none    NA   NA

What I want to do is change NA to 0 in Var 1, if Suppressed=none. I tried

df$Var1<-if (df$suppressed=='none'&is.na(df$Var1)) 0 
         else df$Var1

and got

Error in if (df$suppressed == "none" & is.na(df$Var1)) 0 else df$Var1 : 
  argument is of length zero

Is there something wrong with my if else statement, or is there another way to do this?

Here's the structure of my data:

structure(list(suppressed = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 2L, 2L), .Label = c("full", "none", "partial"), class = "factor"), var1 = c(2, NA, 3, NA, 2, 3, 3, NA, 2, NA), var2 = c(6, 6, 7, NA, 6, 6, NA, 6, 7, NA)), .Names = c("suppressed", "var1", "var2"), row.names = c(NA, -10L), class = "data.frame")

How do I replace specific values with NA in R?

Using R replace() function to update 0 with NA R has a built-in function called replace() that replaces values in a vector with another value, for example, zeros with NAs.

How do I replace specific values in R?

To replace a column value in R use square bracket notation df[] , By using this you can update values on a single column or on all columns. To refer to a single column use df$column_name .

How do you replace values in a vector with Na?

If na is a named vector and as. tag = FALSE , the names indicate variable names, and the associated values indicate those values that should be replaced by NA in the related variable. For instance, set_na(x, na = c(v1 = 4, v2 = 3)) would replace all 4 in v1 with NA and all 3 in v2 with NA .

How do I replace missing values in NA with R?

How to Replace Missing Values(NA) in R: na..
mutate().
Exclude Missing Values (NA).
Impute Missing Values (NA) with the Mean and Median..