0

A dataset I am working with has NA values as \\"\\" I need to recode this into NA in order to collapse some variables but I have not been able to escape the double quotes successfully. I have tried treating the variable as characters and used mutate(x = case when(X == ' \\"\\" ' ~ NA)) as well as mutate(x = fct_recode(x, NULL = '\\"\\"' ) with no luck as it recognizes all values as NA's once I do both approaches. The values I am interested in keeping both start with " and end with " but have numerical values in between.

Any help would be greatly appreciated. Thank you in advance.

camille
  • 16,432
  • 18
  • 38
  • 60
  • `x <- c(1, "\"\"", 2, "\"\"", 3);x[x %in% "\"\""] <- NA;x`? Where's exactly your problem? – jay.sf Sep 22 '20 at 15:37
  • the dataset has 50k observations and multiple variables where \"\" needs to be replaced with NA, is there a way to do this replacement for the entire dataset at once? – Sebastian GM Sep 22 '20 at 15:44
  • 1
    Try `lapply(dat, function(x) {x[x %in% "\"\""] <- NA;x})`. – jay.sf Sep 22 '20 at 15:48
  • is x in function(x) the variable name? if so it does not seem to be working – Sebastian GM Sep 22 '20 at 16:01
  • No, it's just anonymous `x`, just replace `dat` with your data frame name. BTW you probably would get more enthusiastic answers when you put in some effort and make a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – jay.sf Sep 22 '20 at 16:03
  • 1
    thanks a lot! I'll be sure to do so in the future, first day in stack overflow. Have a good day! – Sebastian GM Sep 22 '20 at 16:10

0 Answers0