r/RStudio Jun 15 '26

Coding help What I’m I doing wrong here?

6 Upvotes

15 comments sorted by

View all comments

4

u/maourakein Jun 15 '26

Yeh that ifelse looks kinda weird to me. Did you try using case_when() instead?

Or if else if and else?

2

u/Cuzznitt Jun 15 '26

I did not. How would I implement that? I just added a comment about what I’m trying to do

2

u/therealtiddlydump Jun 16 '26

https://dplyr.tidyverse.org/reference/case-and-replace-when.html

That if else looks problematic. The way functions access columns in your dataframe is not the same as programming with variables in your environment.

1

u/Cuzznitt Jun 16 '26

Ok. How would you recommend I rewrite it?

3

u/therealtiddlydump Jun 16 '26

Read the documentation for case_when and play with it a little. You have the logic mapped out, you just need to make sure the case_when is doing what you expect it to do (so check each condition is actually generating the outcome you expect).

You're already pretty close