0

I am trying to split a numeric variable (Age) within the AdultUCI dataset into two groups. One that is <50 and one that is >=50. I cannot get past how to do this. I have tried a number of different things, but all of the code returns an error message.

adult[1:2,]
adult[[ "age"]] <- ordered(cut(adult[[ "age"]], c(5,25,45,65,100)), labels = c("<50", ">=50"))

Chuck
  • 1
  • There are several ways to do this in R. Try using `ifelse()`. First, check class of your variable to make sure it is numeric (i.e., `class(adult$age)`. If it is not numeric you'll need to convert it. Then you can use `if else`: something like `adult$age <- if else(adult$age < 50, 0, 1)`. Assuming `adult` is the name of your dataframe. – Andrew Apr 20 '19 at 02:10
  • 1
    Also, thanks for posting what you attempted! In the future, you'll get more helpful responses if you also post a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with your desired output. Good luck ,and follow-up if you have more questions. – Andrew Apr 20 '19 at 02:23
  • Couple of items, you should not assign the cut values to the same column you are trying to divide. In your cut statement you are defining 5 cut points but only 2 labels. Your beaks should be c(0, 50, 100). – Dave2e Apr 20 '19 at 04:42
  • That worked! thank you Andrew. You were correct, the "age" age variable was listed as an integer, so I used the following to create the object "Age" as a numeric value. #then ran the code provided >adult$age <- ifelse(adult$age < 50, 0, 1) >Age <- as.numeric(adult$age) – Chuck Apr 20 '19 at 15:14

0 Answers0