0

How do you write the statement "between 0 and 15" R? I am transforming a variable into a categorical variable, and the requirement for one of the categories is to recode the new categorical variable so that the old variable (eg. X) becomes new.variable <- X between 0 and 15.

EDIT - clarification of question

I have been given a data set and the instruction are:

Growth patterns are generated by transforming the X variable into a new categorical variable, which can be named "growth". First category is assigned to islands in the X variable that are between 15 to 50. So that is the question, but my main headache is how to write "between 15 to 50" in R language. This is what I have:

growth$mediumgrowth.islands <- growth$SasiaUrban.X[growth$SasiaUrban.X ???]
Simon MᶜKenzie
  • 8,344
  • 13
  • 50
  • 77

1 Answers1

1

It's not entirely clear to me what you want to achieve, but I believe you want cut:

x <- 0:20
cut(x,c(-Inf,3,9,18,Inf))
# [1] (-Inf,3]  (-Inf,3]  (-Inf,3]  (-Inf,3]  (3,9]     (3,9]     (3,9]     (3,9]     (3,9]     (3,9]     (9,18]    (9,18]    (9,18]    (9,18]    (9,18]    (9,18]   
# [17] (9,18]    (9,18]    (9,18]    (18, Inf] (18, Inf]
# Levels: (-Inf,3] (3,9] (9,18] (18, Inf]

Or possibly findInterval:

findInterval(x,c(-Inf,3,9,18,Inf))
#[1] 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4
Roland
  • 127,288
  • 10
  • 191
  • 288