1

I'm very new to all of this and am (trying!) to complete an assignment for a quantitative methods module at uni. I want to recode a quantitative variable measuring number of minutes spent doing an activity. I want to create a new variable with five categories up to 1300 minutes using the case_when function.

I've tried numerous times to do this and it's just not working. R is only reading the first line of code and wont create a new variable.

Here's my code:

italy$nwspol_re <- case_when(italy$nwspol < 260 ~ "0-260 mins",
                             italy$nwspol >= 261 & italy$nwspol < 520 ~ "261-520", 
                             italy$nwspol >= 521 & italy$nwspol < 780 ~ "521-780",
                             italy$nwspol >= 781 & italy$nwspol < 1040 ~ "781-1040", 
                             italy$nwspol >= 1041 & italy$nwspol < 1300 ~ "1041-1300")

in the console, R just returns the first line and nothing else happens.

I have also tried the mutate function as shown, however R just returns the first line containing italy and nothing else happens, i.e., no new variable is created.

italy %\>%
mutate(nwspol_re = case_when(nwspol \>= 260 \~ '0-260 minutes'
,nwspol \>= 520 & nwspol \< 780 \~ '521-780'
,nwspol \>= 781 & nwspol \< 1040 \~ '781-1040'
,nwspol \>= 1041 & nwspol \< 1285 \~ '1041-1300'
,TRUE \~ 'F'
)
)

Any help would be appreciated.

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 2
    Welcome to SO. You maximise your chance of getting a useful answer if you provide a minimal reproducible example. [This post](https://stackoverflow.com/help/minimal-reproducible-example) may help. Here, we need to see (a sample of) your data, ideally using `dput()` or `dput(head())`. – Limey Jan 09 '23 at 13:16
  • 2
    Welcome to SO, rosieshcode1! First, not sure what is causing it, but R code in SO code blocks shouldn't use backslashes in places like that, perhaps the copy/paste changed it? Second, Please make this question *reproducible*. This includes sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. Thanks! – r2evans Jan 09 '23 at 13:16
  • 3
    If it's more about creating categorical variable than using `case_when` , you might want to check out `cut()` [Convert Numeric to Factor] – margusl Jan 09 '23 at 13:26
  • How are those code blocks executed? Assuming `italy$nwspol` is numeric, your first block evaluates just fine and result is (kind of) expected when generating sample data with `italy <- data.frame(nwspol = sample(0:1300, size = 100, replace = T))` . Though those used intervals do not cover the whole range, e.g. `260 < nswpol < 261` , `520 < nswpol < 521` etc are left out. When removing all those backslahes forum your 2nd block it also works, though it returns only the 1st or final condition as first `>=` should probably read `<=`. – margusl Jan 13 '23 at 11:18

0 Answers0