質問: add a column with an if statement hi i would like to add a column (agecat) based on the first column (age) of this data: age sex cp trestbps chol fbs restecg thalach exang oldpeak slope ca thal num 1 63 1 1 145 233 1 2 150 0 2.3 3 0 6 0 2 67 1 4 160 286 0 2 108 1...
回答: hi @22623639, welcome to rstudio community. you need to use the vectorized ifelse() instead. a <- data.frame(age = c(63, 67, 67, 37, 41, 56, 62, 57, 63, 53)) print(a) #> age #> 1 63 #> 2 67 #> 3 67 #> 4 37 #> 5 41 #> 6 56 #> 7 62 #> 8 57 #> 9 63 #> 10 53 a$agecat <- ifelse(...