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

case_when inserting NAs instead of new assigned values despite all levels are included #7032

Closed
GabryS3 opened this issue May 23, 2024 · 3 comments

Comments

@GabryS3
Copy link

GabryS3 commented May 23, 2024

Hi,
I have been using case_when without any issue so far.
However, I am having an issue at the moment and can;t figure out if it's a bug or something i am doing wrong, as I receive no error from running the function.

So, I have a dataframe with the following column "Location" with 6 levels:

class(Metadata) # data.frame
class(Metadata$"Catch Location") # factor

levels(Metadata$"Catch Location") 
[1] "1A (Moreton North)"  "A2 (Moreton South)"  "B1 (Cooktown North)" "B2 (Cooktown South)"    "C1 (GOC North)"    "C2 (GOC South)" 

When I try to add a new field (column) called "pop" with mutate and case_when, I get all NAs in the pop column, why?
I use the following code:

Metadata = Metadata %>% mutate(pop = (case_when("Catch Location" == "1A (Moreton North)" ~ "1A_Moreton North",
                                                           "Catch Location" == "A2 (Moreton South)" ~ "A2_Moreton South",
                                                           "Catch Location" == "B1 (Cooktown North)" ~ "B1_Cooktown North",
                                                           "Catch Location" == "B2 (Cooktown South)" ~ "B2_Cooktown South",
                                                           "Catch Location" == "C1 (GOC North)" ~ "C1_GOC North",
                                                           "Catch Location" == "C2 (GOC South)" ~ "C2_GOC South")))

Why is it giving me all NAs in the pop field? I have used the exact same code before withou isssues, so I am not sure what is happening.
I tried with Location as "character" or "factor" and it makes no difference...always same issue...NAs in all pop column.

I would appreciate any feedback and help!
Thanks!
Gabry

@joranE
Copy link
Contributor

joranE commented May 23, 2024

I think you need to put Catch Location on the LHS in backticks not quote marks. i.e.

case_when(`Catch Location` == 

Otherwise it's interpreting the LHS as the literal string "Catch Location" which certainly is never equal to any of the values you compare it to.

...or avoid using column names with spaces in them so you can refer to the columns as bare column names.

@DavisVaughan
Copy link
Member

Yea @joranE is right here!

@GabryS3
Copy link
Author

GabryS3 commented May 24, 2024

Thank you so much! that fixed it!!

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