Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace values throughout a tibble

Tags:

r

dplyr

tidyverse

In base R I can use the following code to remove/replace values throughout a dataframe (e.g., all NAs or values greater than 0.99).

df[df > 0.99] <- NA
df[is.na(df)] <- 0L

Is there a way to perform the equivalent operation using dplyr in the tidyverse?

like image 289
Joe Avatar asked Nov 01 '17 17:11

Joe


1 Answers

You can use mutate_all with replace:

df = data.frame(x = c(1.2, 0.4, NA, 0.6), y = c(NA, 0.3, 0.992, 0.5))
df %>% mutate_all(~ replace(., . > 0.99 | is.na(.), 0))
#    x   y
#1 0.0 0.0
#2 0.4 0.3
#3 0.0 0.0
#4 0.6 0.5

Or use funs:

df %>% mutate_all(funs(replace(., . > 0.99 | is.na(.), 0)))

If you just need to integrate this as part of the pipe, replace method should work:

df %>% replace(. > 0.99 | is.na(.), 0)
like image 66
Psidom Avatar answered Oct 19 '22 21:10

Psidom