I have a data that looks like this
| x | y |
|---|---|
| 3 | f |
| 14 | f |
| 1 | f |
| 7 | f |
What I want is to change the y value of 3 individuals with the lowest x value from f to t so my data would look like this:
| x | y |
|:---:|:---:|
| 3 | t |
| 14 | f |
| 1 | t |
| 7 | t |
Kind regards
>Solution :
You could do this as a one-liner in base R:
within(df, y <- ifelse(rank(x) < 4, "t", "f"))
#> x y
#> 1 3 t
#> 2 14 f
#> 3 1 t
#> 4 7 t
Data taken from question
df <- data.frame(x = c(3, 14, 1, 7), y = "f")