Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

How can I change a number to a word in a column of a list in r?

I would like to know how I can change for instance the BMI 26 to "overweight"? I want to change the list.

Should it be like this? .. is it possible with "ifelse" or another function?

underweight = BMI < 18.5,
normal = between(BMI, 18.5, 24.9),
overweight = between(BMI, 25, 29.9),
obese = between(BMI, 30, 34.9),
extreme = BMI > 35)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here's a dplyr::case_when that should work. You were close, but simply need to get the syntax correct. This will create a new column with these weight_class values applied based on the BMI. The final TRUE argument means if anything didn't fall into a group it will receive the value of unknown.

df <- data %>%
mutate(weight_class = case_when(
   BMI < 18.5 ~ 'underweight',
   between(BMI, 18.5, 24.9) ~ 'normal',
   between(BMI, 25, 29.9) ~ 'overweight',
   between(BMI, 30, 34.9) ~ 'obese',
   BMI > 35 ~ 'extreme',
   TRUE ~ 'unknown'
))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...