Home > front end >  How can you mutate (empty strings) as NA in only one variable?
How can you mutate (empty strings) as NA in only one variable?

Time:01-05

How can you transform empty strings to NA in a variable?

So the context is I have 4 datasets that are combined (4 surveys). In one of those datasets and in one variable in particular, the "NA" data are classified as (empty strings). How do I modify/transform/mutate those (empty strings) as NA without affecting the rest of the dataset?

What I saw that I think could work is this:

dat <- dat %>% mutate_all(na_if, "")

The problem is obviously that this selects all the data.

What worked was

nav <- c('', ' ')
ECEMD <- transform(ECEMD, CHILDREN_DIM = replace (CHILDREN_DIM, CHILDREN_DIM %in% nav, NA))

CodePudding user response:

Assuming sort of this data.

dat
#    want_na non_na
# 1        A      A
# 2        B      B
# 3                
# 4        D      D
# 5                
# 6        F      F
# 7                
# 8        H      H
# 9        I      I
# 10       J      J

Then define a vector containing all values you want to with NA, e.g.

nav <- c('', ' ')

and replace them in a defined variable, "want_na" in this example.

dat <- transform(dat, want_na=replace(want_na, want_na %in% nav, NA))
dat
#    want_na non_na
# 1        A      A
# 2        B      B
# 3     <NA>       
# 4        D      D
# 5     <NA>       
# 6        F      F
# 7     <NA>       
# 8        H      H
# 9        I      I
# 10       J      J

Data:

dat <- structure(list(want_na = c("A", "B", "", "D", "", "F", "", "H", 
"I", "J"), non_na = c("A", "B", "", "D", "", "F", "", "H", "I", 
"J")), class = "data.frame", row.names = c(NA, -10L))

CodePudding user response:

What worked was

nav <- c('', ' ')
ECEMD <- transform(ECEMD, CHILDREN_DIM = replace (CHILDREN_DIM, CHILDREN_DIM %in% nav, NA))
  •  Tags:  
  • Related