Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

if statement - Create a new column based on conditional inclusion fulfilling conditions in R

My dataset looks like this:

df <- data.frame(PatientID = c("3454","345","5","345","567","79"), sex = c(Female, Female, Female, Male, Male, Male)
                 waist = c(60, 89, 90, 110, 200, 150),  tryglicerides = c(100, 150, 170, 105, 200, 140),HDL = c(50, 41, 20, 37, 30, 40), hypertension = c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE), diabetes = c(FALSE, FALSE, TRUE, TRUE, TRUE, FALSE), stringsAsFactors = F)

Could you help me coding this in R please?

Thanks!

I need to create a new column that will be called METABOLIC_SYNDROME. For the participant to be TRUE in the column METABOLIC_SYNDROME needs to fulfil 3/5 of these conditions:

  • Waist: ≥89 in Female, ≥102 in Male
  • Trygliceride: ≥150
  • HDL: ≤40 Male, ≤50 Female
  • Hypertension: TRUE
  • Diabetes: TRUE
question from:https://stackoverflow.com/questions/65843453/create-a-new-column-based-on-conditional-inclusion-fulfilling-conditions-in-r

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

1 Reply

0 votes
by (71.8m points)

You could sum 5 boolean expressions covering all your requirements:

waist <- (df$sex == 'Female' & df$Waist >= 89) | (df$sex == 'Male' & df$Waist >= 102)
trygliceride <- df$tryglicerides >= 150
hdl <- (df$sex == 'Female' & df$HDL <= 50) | (df$sex == 'Male' & df$HDL <= 40)
hypertension <- df$hypertension
diabetes <- df$diabetes

df$METABOLIC_SYNDROME <- waist + trygliceride + hdl + hypertension + diabetes >= 3

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...