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

Comments

@xuhuizhang
Copy link

@xuhuizhang 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 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. 🤔

Loading

@xuhuizhang
Copy link
Author

@xuhuizhang xuhuizhang commented Sep 7, 2018

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

Loading

@hadley
Copy link
Member

@hadley hadley commented Jan 4, 2019

Seems reasonable to add it here.

Loading

@kbodwin
Copy link
Contributor

@kbodwin kbodwin commented Jan 19, 2019

I'll give it a go!

Loading

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 hadley closed this in #170 Feb 16, 2019
hadley added 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants