'How to switch from a continuous to a factor variable in R
I have a data set with a continuous variable, and I want to split it up into factors.
This is my code so far:
data$pgr<-as.character(data$pgr)
data$pgr[data$pgr <= 7] <- "<= 7 progesterone receptors"
data$pgr[data$pgr > 7 & data$pgr <= 32.5] <- "7-32.5 progesterone receptors"
data$pgr[data$pgr > 32.5 & data$pgr <= 131.8] <- "32.5-131.8 progesterone receptors"
data$pgr[data$pgr > 131.8] <- "> 131.8 progesterone receptors"
data$pgr<-as.factor(data$pgr)
The thing is, it worked for a different variable when I only used one double inequality but won't work for this one? The <=7 and >131.8 both work.
I thought you have to use a double && but, for my other variable it only worked with a singular &. For this one, it works for neither.
Please could someone explain this to me/how to change my code? I would appreciate it.
Solution 1:[1]
cut is your friend:
data <- data.frame(pgr = runif(100, 1, 200))
data$pgr <- cut(data$pgr,
breaks = c(-Inf, 7, 32.5, 131.8, Inf),
labels = c("<= 7 progesterone receptors",
"7-32.5 progesterone receptors",
"32.5-131.8 progesterone receptors",
"> 131.8 progesterone receptors"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
