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

Parse numbers produces only NAs #1454

Closed
Leprechault opened this issue Nov 30, 2022 · 2 comments
Closed

Parse numbers produces only NAs #1454

Leprechault opened this issue Nov 30, 2022 · 2 comments

Comments

@Leprechault
Copy link

Hi Everyone!

 library(dplyr)
 library(readr)
    
    # Read in the data
    ds <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/parse_problem.csv")
    str(ds)
    # 'data.frame':	105 obs. of  5 variables:
    #  $ Especie      : chr  "C_externa" "C_externa" "C_externa" "C_externa" ...
    #  $ Rep          : chr  "R1" "R2" "R3" "R4" ...
    #  $ Instar_IN    : chr  "1Instar" "1Instar" "1Instar" "1Instar" ...
    #  $ Instar_PS    : chr  "ovo" "ovo" "ovo" "ovo" ...
    #  $ Tentou_predar: int  3 25 20 36 12 0 1 10 0 14 ...

I'd like to parse_number using mutate for Especie factor for plotting purposes. I've tried the following:

    ds.new <- ds %>% mutate(x_1=readr::parse_number(Especie),group = Especie)
    summary(ds.new$x_1)
    #    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
    #      NA      NA      NA     NaN      NA      NA     105 

I don't know why it's not working and I've just NA. Please, any ideas?

@joranE
Copy link

joranE commented Nov 30, 2022

That function is for extracting numbers stored as characters, e.g. turning "45" into the integer 45. It looks like Especie doesn't have any numbers in it, and additionally isn't a factor, but a character vector. It sounds like you want the underlying integer codes from a factor. If so, you could explicitly coerce it to a factor (as.factor()) and then integer (as.integer()), i.e. mutate(x_1 = as.integer(as.factor(Especie))).

@hadley hadley closed this as completed Jul 31, 2023
@hadley
Copy link
Member

hadley commented Jul 31, 2023

Thanks @joranE!

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

3 participants