-1

My data is

   Age
1:  13
2:   4
3:  14
4:  52
5:  63
6:  25
7:  53

I want a variable stating the following characterization: If age 1-13 -> e, if age 14-25 -> b, if age 26-100 -> d

Desired output

   Age Characterization
1:  13                e
2:   4                e
3:  14                b
4:  52                d
5:  63                d
6:  25                b
7:  53                d

Input

structure(list(Age = c(13L, 4L, 14L, 52L, 63L, 25L, 53L)), row.names = c(NA, 
-7L), class = c("data.table", "data.frame"))
user438383
  • 5,716
  • 8
  • 28
  • 43

1 Answers1

0

Try findInterval like below

within(
  df,
  Characterization <- c("e", "b", "d")[1 + findInterval(Age, c(13, 25, 100), left.open = TRUE)]
)

which gives

  Age Characterization
1  13                e
2   4                e
3  14                b
4  52                d
5  63                d
6  25                b
7  53                d

Data

> dput(df)
structure(list(Age = c(13L, 4L, 14L, 52L, 63L, 25L, 53L)), row.names = c(NA, 
-7L), class = "data.frame")
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
  • Thomas, what's the significance of "1 +" before findInterval function? Without which I get a vector of length 5 for same age vector. – Karthik S Oct 15 '20 at 12:00
  • 2
    @KarthikS `findInterval` returns values starting from `0`, I need `+1` since I want it as indices for vector `c("e","b","d")` – ThomasIsCoding Oct 15 '20 at 12:02
  • Please search before answering, this has been asked million times. – zx8754 Oct 15 '20 at 12:10