Search code examples
rif-statementconditional-statementsconditional-formattingexclusion-constraint

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

Solution

  • 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