Last active
August 29, 2015 14:01
-
-
Save jennybc/7722cb734d9c650f9f85 to your computer and use it in GitHub Desktop.
Demonstrate dplyr::group_by() error when a variable has class 'AsIs'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
## accept default behavior, i.e. stringsAsFactors = TRUE | |
## downside is that 'name' really shouldn't be a factor | |
df <- data.frame(name = c("jenny", "jim", "reed", "lorrie"), | |
gender = c("female", "male", "male", "female")) | |
df %>% group_by(gender) %>% summarize(count = n()) # this works | |
## protect a single character variable with I() | |
df <- data.frame(name = I(c("jenny", "jim", "reed", "lorrie")), | |
gender = c("female", "male", "male", "female")) | |
df %>% group_by(gender) %>% summarize(count = n()) # does not work! | |
# Error: column 'name' has unsupported type | |
## global protection | |
## downside is that 'gender' really should be a factor | |
df <- data.frame(name = c("jenny", "jim", "reed", "lorrie"), | |
gender = c("female", "male", "male", "female"), | |
stringsAsFactors = FALSE) | |
df %>% group_by(gender) %>% summarize(count = n()) # this works | |
## retrospective character protection | |
## downside is that this is annoying | |
df <- data.frame(name = c("jenny", "jim", "reed", "lorrie"), | |
gender = c("female", "male", "male", "female")) | |
df$name <- as.character(df$name) | |
df %>% group_by(gender) %>% summarize(count = n()) # this works |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment