2

I have a tibble object and I want to replace all of the columns except certain ones with a value (say NA or 0). I can able to do it without pipes %>% but how can I do it with %>%?

library(tibble)  
dtf <- tibble(id = c('12', '22', '33', '40'),
              x1 = c(0, 2, 3, 4),
              a2 = c(1, 0, 3, 0),
              c5 = c('a', 'b', 'c', 'd'))

# This raises an error:
dtf %>% select(-id) <- NA

Error in dtf %>% select(-id) <- NA : could not find function "%>%<-"

# This works:
dtf[, colnames(dtf) != 'id'] <- NA
dtf
# A tibble: 4 x 4
  id    x1    a2    c5   
  <chr> <lgl> <lgl> <lgl>
1 12    NA    NA    NA   
2 22    NA    NA    NA   
3 33    NA    NA    NA   
4 40    NA    NA    NA 

I believe I should use mutate() or mutate_all() but I couldn't figure out. One similar SO answer offered na_if() for NA values but I cannot make it work for this case.

HBat
  • 4,873
  • 4
  • 39
  • 56
  • It is already `NA` Not clear – akrun Jan 27 '20 at 16:21
  • If you're running this code straight through, you've already assigned those values to become NA by the time you get to the `select` call. Any reason why you have to use pipes and can't just use the base solution? Asking because I've seen a lot of SO posts that insist on not relying on base R but without any reasoning – camille Jan 27 '20 at 16:29
  • Also `na_if` is for replacing a value with `NA` *if* it is some other value. In this case, you're just trying to make everything NA, not depend on any condition, right? – camille Jan 27 '20 at 16:30
  • Edited the code in order to make it clear. It was confusing. Correct, I want to make everything NA, or some other value, without any condition. – HBat Jan 27 '20 at 16:31

3 Answers3

1

If the intention is to update the columns, use mutate_at

library(dplyr)
dtf <- dtf %>% 
           mutate_at(vars(-id), ~ NA)

If we need to replace with 0

dtf <- dtf %>%
          mutate_at(vars(-id), replace_na, 0)
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    The first one is the one I wanted. I want to set a single value to all columns, it can be all `NA`s or some other value. Thanks! – HBat Jan 27 '20 at 16:28
1

Depends what you want to do.

  1. Mutate all variables in the same way and select by name/place:
df %>%
mutate_at(.vars = c("x1","a2","c5"), funs(case_when(is.na(.) ~ 0,TRUE ~ .)))

# or 

df %>%
mutate_at(.vars = -id, funs(case_when(is.na(.) ~ 0,TRUE ~ .)))
  1. Mutate all variables in the same way and select by type:
df %>%
mutate_if(is.numeric, funs(case_when(is.na(.) ~ 0,TRUE ~ .)))
Fnguyen
  • 1,159
  • 10
  • 23
0

you just do dtf[,colnames(dtf) != "id"] <- NA in one go... this will replace all column values except id with NA

Vishal Katti
  • 532
  • 2
  • 6