It seems mutate_if
modifies all columns if .predicate
returns an empty number of columns
sampleData <- tibble(numbers = 1:10, factors = factor(sample(letters[1:3], size = 10, replace = T)))
# does work
sampleData %>%
mutate_if(is.factor, funs(length)) # whaterever function
# does work
sampleData %>%
mutate_if(is.numeric, funs(mean)) # whaterever function
# does not work -> expect no changes to factor column
sampleData %>%
select(- numbers) %>%
mutate_if(is.numeric, funs(mean)) # whaterever function
# does not work -> expect no changes to numbers column
sampleData %>%
select(- factors) %>%
mutate_if(is.factor, funs(length)) # whaterever function
Using dplyr * 0.5.0 2016-06-24 CRAN (R 3.3.1)
Duplicates of #2181, #2009, #1989 and already fixed in dev version by #2011 .
library(dplyr) # dev version 0.5.0.9000
sampleData <- tibble::tibble(numbers = 1:10, factors = factor(sample(letters[1:3], size = 10, replace = T)))
# does work
sampleData %>%
mutate_if(is.factor, funs(length))
#> # A tibble: 10 x 2
#> numbers factors
#> <int> <int>
#> 1 1 10
#> 2 2 10
#> 3 3 10
#> 4 4 10
#> 5 5 10
#> 6 6 10
#> 7 7 10
#> 8 8 10
#> 9 9 10
#> 10 10 10
# does work
sampleData %>%
mutate_if(is.numeric, funs(mean))
#> # A tibble: 10 x 2
#> numbers factors
#> <dbl> <fctr>
#> 1 5.5 b
#> 2 5.5 b
#> 3 5.5 a
#> 4 5.5 b
#> 5 5.5 a
#> 6 5.5 a
#> 7 5.5 a
#> 8 5.5 a
#> 9 5.5 a
#> 10 5.5 c
# does work -> expect no changes to factor column
sampleData %>%
select(- numbers) %>%
mutate_if(is.numeric, funs(mean))
#> # A tibble: 10 x 1
#> factors
#> <fctr>
#> 1 b
#> 2 b
#> 3 a
#> 4 b
#> 5 a
#> 6 a
#> 7 a
#> 8 a
#> 9 a
#> 10 c
# does work -> expect no changes to numbers column
sampleData %>%
select(- factors) %>%
mutate_if(is.factor, funs(length))
#> # A tibble: 10 x 1
#> numbers
#> <int>
#> 1 1
#> 2 2
#> 3 3
#> 4 4
#> 5 5
#> 6 6
#> 7 7
#> 8 8
#> 9 9
#> 10 10
Works for me as advertised by @cderv in the dev version.
@cderv thanks for collecting the related issues together!
Most helpful comment
@cderv thanks for collecting the related issues together!