How to assign levels based on numerical value

Greetings,

I have dataset containing lap times. A small sample of the dataset is shown below:

1.148341719 1.149355031 1.15 1.150142468 1.151574093 1.154324165 1.154833376 1,2855 1.156342757 1.156512739
1.15694131 1.156984764 1.158113329 1.159175428 1.16 1.253106228 1.16428044

The average lap time 1,24 minutes.

Now I want to create a binary variable , containing the levels fast (quicker than 1,24 minutes) and slow (slower than 1,24),but I don't know how to assign the levels based on the numerical value of the datapoints

I basically want the output to look like this:

fast fast fast fast fast fast fast
slow fast fast
fast fast fast fast fast slow fast slow fast

I would be so so grateful if someone of you could please help me with creating the variable.

Try santoku

library(tidyverse)
library(santoku)
#> Warning: package 'santoku' was built under R version 4.0.2
#> 
#> Attaching package: 'santoku'
#> The following object is masked from 'package:tidyr':
#> 
#>     chop
#details of santoku at

times <- c(1.148341719, 1.149355031,1.15,
           1.150142468, 1.151574093, 1.154324165, 1.154833376,
           1.156342757, 1.156512739,
           1.15694131, 1.156984764, 1.158113329,
           1.159175428, 1.16, 1.163106228, 1.16428044)

times_2 <- chop_equally(times, 2)
times_2
#>  [1] [0%, 50%)   [0%, 50%)   [0%, 50%)   [0%, 50%)   [0%, 50%)   [0%, 50%)  
#>  [7] [0%, 50%)   [0%, 50%)   [50%, 100%] [50%, 100%] [50%, 100%] [50%, 100%]
#> [13] [50%, 100%] [50%, 100%] [50%, 100%] [50%, 100%]
#> Levels: [0%, 50%) [50%, 100%]

Created on 2020-07-06 by the reprex package (v0.3.0)

Or in base R, convert a logical expression to integer to give you a binary variable.

times <- c(1.148341719, 1.549331,1.15,
           1.150142468,1.151573,1.154324165,1.154833376,
           1.156342757,1.4565139,
           1.35694131, 1.156984764,1.158113329,
           1.159175428,1.16,1.163106228,1.16428044)

as.integer(times > 1.24)
#>  [1] 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0

Created on 2020-07-07 by the reprex package (v0.3.0)

1 Like