Computing variables using Conditional statements

Hi I have a data set with few variables.

AGE Gender Smoking DM TC HDL BP
30 f no Low 2 4 2
30 m no Low 3 4 1
30 f no Low 3 4 1
30 f no Low 4 4 1
30 f no Low 3 4 1
30 m yes Low 1 4 1
30 f no Low 3 4 1
30 m no Low 2 4 4
30 m no Low 2 2 2
30 m no Low 3 1 1
30 f no Low 3 5 1
30 f no High 3 2 3
30 m no Low 3 2 3
30 m no Low 3 2 2
30 f no Low 3 4 3
30 m no Low 5 4 1
30 m no High 1 5 4
30 m no Low 5 2 2
30 f no Low 3 4 1
30 f no Low 3 4 3
30 f no Low 3 2 1
30 f no Low 4 4 1
30 f no Low 3 4 1

I have 2 seperate equations for male and female. They go as if male

A male= Age*5 + 1(if TC=1)+2(ifTC=2) +3(if TC=3) +1(HDL=1)+2(HDL=2)+3(HDL=3) +5(if smoking) +8(if DM=High)

How am I to tackle this. I am new to r and I can see no way around?
I want to create IFELSE statements and solve this equation and get the replies to a seperate column

Hi @rasuranasinghe,
You can calculate a combined "risk" value from your data using a set of nested ifelse() functions, like this:

df <- read.table(header=TRUE, text=c("
AGE Gender  Smoking DM  TC  HDL BP
30  f   no  Low 2   4   2
30  m   no  Low 3   4   1
30  f   no  Low 3   4   1
30  f   no  Low 4   4   1
30  f   no  Low 3   4   1
30  m   yes Low 1   4   1
30  f   no  Low 3   4   1
30  m   no  Low 2   4   4
30  m   no  Low 2   2   2
30  m   no  Low 3   1   1
30  f   no  Low 3   5   1
30  f   no  High    3   2   3
30  m   no  Low 3   2   3
30  m   no  Low 3   2   2
30  f   no  Low 3   4   3
30  m   no  Low 5   4   1
30  m   no  High    1   5   4
30  m   no  Low 5   2   2
30  f   no  Low 3   4   1
30  f   no  Low 3   4   3
30  f   no  Low 3   2   1
30  f   no  Low 4   4   1
30  f   no  Low 3   4   1
"))

head(df)
#>   AGE Gender Smoking  DM TC HDL BP
#> 1  30      f      no Low  2   4  2
#> 2  30      m      no Low  3   4  1
#> 3  30      f      no Low  3   4  1
#> 4  30      f      no Low  4   4  1
#> 5  30      f      no Low  3   4  1
#> 6  30      m     yes Low  1   4  1

# For males, and asssuming you want to include BP risk
df$risk <- ifelse(df$Gender == "m",
                  {df$AGE*5 + df$TC + df$HDL + df$BP + 
                  ifelse(df$Smoking == "yes", 5, 0) +
                  ifelse(df$DM == "High", 8, 0)}, "not_male")

head(df)
#>   AGE Gender Smoking  DM TC HDL BP     risk
#> 1  30      f      no Low  2   4  2 not_male
#> 2  30      m      no Low  3   4  1      158
#> 3  30      f      no Low  3   4  1 not_male
#> 4  30      f      no Low  4   4  1 not_male
#> 5  30      f      no Low  3   4  1 not_male
#> 6  30      m     yes Low  1   4  1      161

# Possible equation for females
df$risk <- ifelse(df$Gender == "f",
                  {df$AGE*3 + df$TC + df$HDL + df$BP + 
                  ifelse(df$Smoking == "yes", 6, 0) +
                  ifelse(df$DM == "High", 10, 0)}, df$risk)
head(df)
#>   AGE Gender Smoking  DM TC HDL BP risk
#> 1  30      f      no Low  2   4  2   98
#> 2  30      m      no Low  3   4  1  158
#> 3  30      f      no Low  3   4  1   98
#> 4  30      f      no Low  4   4  1   99
#> 5  30      f      no Low  3   4  1   98
#> 6  30      m     yes Low  1   4  1  161

Created on 2021-07-28 by the reprex package (v2.0.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.