質問

I have a variable with patients ages. I have 180 values with ages ranging from 18 to 92 years. I want to use this variable as a factor with three levels:

a: ages from 18-57 b: ages from 58-68 c: ages from 69-92

I typed:

AGE.factor=cut(AGE, breaks=c(18:57,58:68,69:92))

but the response i get is:

str(AGE.factor) Factor w/ 74 levels "(18,19]","(19,20]",..: 44 44 44 44 44 44 50 50 50 28 ...

We did that happen???i only want 3 levels of my variable with the ages grouped.

Thanks

役に立ちましたか?

解決

Instead of grouping values using break function , you can replace the values using recode() function

Try the below code:

## a: ages from 18-57 b: ages from 58-68 c: ages from 69-92

install.package(car)

library(car)

age <- 18:92

age<-recode(age,"18:57='A'")

age<-recode(age,"58:68='B'")

age<-recode(age,"69:92='C'")

factor(age)

factor(age)

## [1] A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A A
## [39] A A B B B B B B B B B B B C C C C C C C C C C C C C C C C C C C C C C C C
## Levels: A B C
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top