0

I am trying to add a new variable to a data set I am working with in R and the data is time-based and distance based (rally racing). I cannot figure out how to add a variable with conditional data based on another variable, and have it all in the same column.

I can create a new column and add it to the data frame, that is not an issue, but I cannot figure out how to label the data.

For example, I know that 0-2 minutes was mile 1, 2-4 minutes was mile 2, etc. But how can I label the data based on conditions like this? I know SQL, in which case I would use a CASE statement. Is there an equivalent in R? I am hoping to do something like:

CASE

WHEN timeseconds >2 THEN 'mile1'

WHEN timeseconds BETWEEN 2 AND 4 THEN 'mile2'

END AS mile_marker

But I don't know what to do in R to make this work. Would this be a bunch of nested if else statements?

gdaem
  • 23
  • 4
  • With `dplyr` you can use `case_when()`. But if everything is based on numeric intervals have a look at the FAQ on [How to bin numeric data?](https://stackoverflow.com/q/5570293/903061) and the `?cut` function might be nice. – Gregor Thomas Mar 14 '23 at 18:10

0 Answers0