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

no applicable method for 'as_factor' applied to an object of class "c('double', 'numeric')" #145

Closed
xuhuizhang opened this issue Sep 7, 2018 · 4 comments · Fixed by #170
Labels
feature a feature request or enhancement tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day

Comments

@xuhuizhang
Copy link

xuhuizhang commented Sep 7, 2018

I tried to categorize a numerical vector by using as_factor()

as_factor(c(1, 2, 3))

The error hapeend.

Error in UseMethod("as_factor") : no applicable method for 'as_factor' applied to an object of class "c('double', 'numeric')"

But it works when comes to as.factor()

as.factor(c(1, 2, 3))
[1] 1 2 3
Levels: 1 2 3

@cderv
Copy link

cderv commented Sep 7, 2018

as_factor methods only exists currently for factor and character. Factor vector are associated with character vector. In base R, if you provide numeric, it will be coerce to character before any other operation. I believe forcats is more strict and let the user to the coercion to prevent any issue. It is why you have to pass character vector only in this package right now. However, you can use forcats on already created factor by the base R factor package if you want. However, there is a order issue. (see below) Again, forcats is strict and keep level in vector ordering.

library(forcats)
# use factor
factor(c(1, 2, 3))
#> [1] 1 2 3
#> Levels: 1 2 3
# but levels are re-ordered
factor(c(1, 3, 2))
#> [1] 1 3 2
#> Levels: 1 2 3
as.factor(c(1, 3, 2))
#> [1] 1 3 2
#> Levels: 1 2 3
as.factor(as.character(c(1, 3, 2)))
#> [1] 1 3 2
#> Levels: 1 2 3
# forcats prevent level re-ordering
as_factor(as.character(c(1, 2, 3)))
#> [1] 1 2 3
#> Levels: 1 2 3
as_factor(as.character(c(1, 3, 2)))
#> [1] 1 3 2
#> Levels: 1 3 2
fct_inorder(as.character(c(1, 3, 2)))
#> [1] 1 3 2
#> Levels: 1 3 2

Created on 2018-09-07 by the reprex package (v0.2.0).<\sup>

Hope it helps clarify the current situation. I let the tidyverse team say if a numeric type support is possible. 😄 maybe once the work in vctrs 📦 is ok. 🤔

@xuhuizhang
Copy link
Author

Aha, a order issue🧐🧐, finally. Many thanks, It was a great help.

@hadley
Copy link
Member

hadley commented Jan 4, 2019

Seems reasonable to add it here.

@hadley hadley added feature a feature request or enhancement tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day labels Jan 4, 2019
@kbodwin
Copy link
Contributor

kbodwin commented Jan 19, 2019

I'll give it a go!

kbodwin added a commit to kbodwin/forcats that referenced this issue Jan 19, 2019
kbodwin added a commit to kbodwin/forcats that referenced this issue Jan 19, 2019
hadley pushed a commit that referenced this issue Feb 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement tidy-dev-day 🤓 Tidyverse Developer Day rstd.io/tidy-dev-day
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants