Home > database >  How can I replace particular values in my data.frame depending on their value?
How can I replace particular values in my data.frame depending on their value?

Time:10-31

I have a dataframe (concerning prostate cancer survival) and one of the factor variables "stage" only has 4 observations for stage 3. I want to combine stage 3 with stage 2 before doing regression. Is there a function that will search through the stage data and if stage = 3, change this value to 2?

Many thanks!

I have tried:

pro$stage[pro$stage==3] <- 2 

but this doesnt seem to do the trick

CodePudding user response:

With a dataframe this should work.

pro <- data.frame(name=seq(1:10),disease=rep("Cancer"),stage=round(runif(n=10,min = 1,max = 3)),survival=round(runif(n=10,min = 0,max = 1)))
pro
pro$stage[pro$stage==3]
pro$stage[pro$stage==3] <- 2
pro

CodePudding user response:

pro$stage <- ifelse(pro$stage==3,2,pro$stage)
  •  Tags:  
  • r
  • Related