I've been trying for a while to replace N/A entries in a data-frame with values of my choice without success. I checked the sources and tried the code below. Can anyone point out why my commands don't work in spite some sources suggesting they should?
The data-frame exampleDF below contains some N/A entries under the column "zacko":
> exampleDF
dates random letters action zacko
1 2018-10-30 00:05:19 10 a go Mickey
2 2018-10-30 13:58:39 2 b run Donald
3 2018-10-31 03:51:59 1 c fly <NA>
4 2018-10-31 17:45:19 10 d sit Goofy
5 2018-11-01 07:38:39 10 e jump Daisy
6 2018-11-01 21:31:59 13 f hike <NA>
7 2018-11-02 11:25:19 6 g dance <NA>
8 2018-11-03 01:18:39 6 h go Dagobert
9 2018-11-03 15:11:59 8 i dance <NA>
10 2018-11-04 05:05:19 6 j run Pluto
11 2018-11-04 18:58:39 2 k sit <NA>
12 2018-11-05 08:51:59 6 l laugh Minnie
13 2018-11-05 22:45:19 3 m cry Gustav
14 2018-11-06 12:38:39 11 n write Reginald
15 2018-11-07 02:31:59 1 o fly <NA>
I looked at Correct syntax for mutate_if and tried to replace these entries with values of my choice accordingly as per
exampleDF %>% mutate_if(is.character, funs(ifelse(is.na(.),
"REPLACEMENT",.)))
Warning message:
funs() is soft deprecated as of dplyr 0.8.0
please use list() instead
# Before:
funs(name = f(.)
# After:
list(name = ~f(.))
> exampleDF %>% mutate_if(is.character, list(ifelse(is.na(.),
"REPLACEMENT",.)))
Error: Can't create call to non-callable object
Call `rlang::last_error()` to see a backtrace
without success (as you can see from the error messages). Interestingly, the commands below work like a charm at the console:
> df <- tibble(x = c(1, 2, NA), y = c("a", NA, "b"), z = list(1:5, NULL,
10:20))
> df
# A tibble: 3 x 3
x y z
<dbl> <chr> <list>
1 1 a <int [5]>
2 2 NA <NULL>
3 NA b <int [11]>
> df %>% replace_na(list(x = 0, y = "unknown"))
# A tibble: 3 x 3
x y z
<dbl> <chr> <list>
1 1 a <int [5]>
2 2 unknown <NULL>
3 0 b <int [11]>
> df %>% mutate(x = replace_na(x, 0))
# A tibble: 3 x 3
x y z
<dbl> <chr> <list>
1 1 a <int [5]>
2 2 NA <NULL>
3 0 b <int [11]>
Why don't the equivalent commands work for my data-frame? See error messages below:
exampleDF %>% replace_na(list(dates = as.POSIXct("2018-10-30 13:58:39"),
random = 5, letters = "a", action = "crying", zacko = "FRUSTRATION"))
dates random letters action zacko
1 2018-10-30 00:05:19 10 a go Mickey
2 2018-10-30 13:58:39 2 b run Donald
3 2018-10-31 03:51:59 1 c fly <NA>
4 2018-10-31 17:45:19 10 d sit Goofy
5 2018-11-01 07:38:39 10 e jump Daisy
6 2018-11-01 21:31:59 13 f hike <NA>
7 2018-11-02 11:25:19 6 g dance <NA>
8 2018-11-03 01:18:39 6 h go Dagobert
9 2018-11-03 15:11:59 8 i dance <NA>
10 2018-11-04 05:05:19 6 j run Pluto
11 2018-11-04 18:58:39 2 k sit <NA>
12 2018-11-05 08:51:59 6 l laugh Minnie
13 2018-11-05 22:45:19 3 m cry Gustav
14 2018-11-06 12:38:39 11 n write Reginald
15 2018-11-07 02:31:59 1 o fly <NA>
Warning messages:
1: In `[<-.factor`(`*tmp*`, !is_complete(data[[var]]), value = "crying") :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, !is_complete(data[[var]]), value =
"FRUSTRATION") :
invalid factor level, NA generated
> exampleDF %>% mutate(zacko = replace_na(zacko, "GAGA"))
dates random letters action zacko
1 2018-10-30 00:05:19 10 a go Mickey
2 2018-10-30 13:58:39 2 b run Donald
3 2018-10-31 03:51:59 1 c fly <NA>
4 2018-10-31 17:45:19 10 d sit Goofy
5 2018-11-01 07:38:39 10 e jump Daisy
6 2018-11-01 21:31:59 13 f hike <NA>
7 2018-11-02 11:25:19 6 g dance <NA>
8 2018-11-03 01:18:39 6 h go Dagobert
9 2018-11-03 15:11:59 8 i dance <NA>
10 2018-11-04 05:05:19 6 j run Pluto
11 2018-11-04 18:58:39 2 k sit <NA>
12 2018-11-05 08:51:59 6 l laugh Minnie
13 2018-11-05 22:45:19 3 m cry Gustav
14 2018-11-06 12:38:39 11 n write Reginald
15 2018-11-07 02:31:59 1 o fly <NA>
Warning message:
In `[<-.factor`(`*tmp*`, !is_complete(data), value = "GAGA") :
invalid factor level, NA generated
I would have expected that my code above works, as per examples given at Correct syntax for mutate_if and examples given under help-file for replace_na(data, replace, ...) (requiring tidyr package).