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 applies fun to all columns if predicate returns all false #2181

Closed
jenswirf opened this issue Oct 17, 2016 · 2 comments
Closed

mutate_if applies fun to all columns if predicate returns all false #2181

jenswirf opened this issue Oct 17, 2016 · 2 comments

Comments

@jenswirf
Copy link

df1 <- data_frame(
  a = 1:10,
  b = runif(10),
  c = letters[1:10]
)

df2 <- data_frame(
  a = 1:10,
  b = runif(10),
  c = 10:1
)

# works as intended, c is converted to a factor
df1 %>% mutate_if(is.character, as.factor)

# all columns become factors
df2 %>% mutate_if(is.character, as.factor)
@cderv
Copy link
Contributor

cderv commented Oct 18, 2016

duplicates of #2009 and #1989 and already fixed in dev version by #2011 .

With dplyr 0.5.0.900, it is working ok.

devtools::dev_mode()
#> Dev mode: OFF
library(dplyr) # dev version 0.5.0.9000

df1 <- data_frame(
  a = 1:10,
  b = runif(10),
  c = letters[1:10]
)

df2 <- data_frame(
  a = 1:10,
  b = runif(10),
  c = 10:1
)

# works as intended, c is converted to a factor
glimpse(df1)
#> Observations: 10
#> Variables: 3
#> $ a <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
#> $ b <dbl> 0.32494503, 0.73370927, 0.90391393, 0.79685123, 0.09798207, ...
#> $ c <chr> "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"
df1 %>% mutate_if(is.character, as.factor) %>% glimpse()
#> Observations: 10
#> Variables: 3
#> $ a <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
#> $ b <dbl> 0.32494503, 0.73370927, 0.90391393, 0.79685123, 0.09798207, ...
#> $ c <fctr> a, b, c, d, e, f, g, h, i, j

# all columns stays the same
glimpse(df2)
#> Observations: 10
#> Variables: 3
#> $ a <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
#> $ b <dbl> 0.67622567, 0.53826781, 0.31735237, 0.99455487, 0.20520285, ...
#> $ c <int> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
df2 %>% mutate_if(is.character, as.factor) %>% glimpse()
#> Observations: 10
#> Variables: 3
#> $ a <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
#> $ b <dbl> 0.67622567, 0.53826781, 0.31735237, 0.99455487, 0.20520285, ...
#> $ c <int> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

@hadley
Copy link
Member

hadley commented Jan 31, 2017

Fixed in #2011

@hadley hadley closed this as completed Jan 31, 2017
@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
None yet
Projects
None yet
Development

No branches or pull requests

3 participants