Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mutate_if does not behave as expected #2208

Closed
burkovae opened this issue Oct 27, 2016 · 3 comments
Closed

mutate_if does not behave as expected #2208

burkovae opened this issue Oct 27, 2016 · 3 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@burkovae
Copy link

burkovae commented Oct 27, 2016

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)

@cderv
Copy link
Contributor

cderv commented Oct 27, 2016

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

@krlmlr
Copy link
Member

krlmlr commented Nov 7, 2016

Works for me as advertised by @cderv in the dev version.

@krlmlr krlmlr added data frame bug an unexpected problem or unintended behavior labels Nov 7, 2016
@hadley hadley closed this as completed Jan 31, 2017
@hadley
Copy link
Member

hadley commented Jan 31, 2017

@cderv thanks for collecting the related issues together!

@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants