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

ddply with .drop=FALSE converts factor .variable to character #81

Closed
BenBarnes opened this issue Apr 24, 2012 · 0 comments
Closed

ddply with .drop=FALSE converts factor .variable to character #81

BenBarnes opened this issue Apr 24, 2012 · 0 comments

Comments

@BenBarnes
Copy link

When using plyr with .drop = FALSE, I find that factor vectors included in the .variables argument are (at least sometimes) converted to character. The R code below illustrates what I mean:

mydf <- data.frame(aa = factor(rep(1:3, each = 4), levels = 1:4), bb = rep(LETTERS[2:5],
  each = 3))
class(mydf$aa) # factor

ans <- ddply(mydf, .(aa), summarize, uLet = length(unique(bb)), .drop = FALSE)
class(ans$aa) # character

ansDrop <- ddply(mydf, .(aa), summarize, uLet = length(unique(bb)), .drop = TRUE)
class(ansDrop$aa) # factor

## End of R code

As is written in the package's NEWS file, as of version 1.5 empty factor levels are indeed preserved when .drop = FALSE. However, the class of the grouping variable(s) seems to vary depending on the value of this argument. It's no big deal to convert the data back to a factor after running ddply, but perhaps an additional modification to the split_labels function to restore factor classes after expand.grid (where stringsAsFactors = FALSE) would provide more consistent output.

All the best

@hadley hadley closed this as completed in 1290d86 Oct 8, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants