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