I'm not an advanced R user but normally soon or later I find the help I need. Well, not this time. I have a data frame called "df" and I'm trying to create an extra column "Sel" where to store information based on other columns info. To do that I used a nested ifelse function, below is the code I used, it works for the first two conditions but not for the second two conditions where I use the AND operator. I don't see any difference where comparing the usage to other examples, and I don't get errors, only the statement relative to that condition is not pasted/printed. (I've also tried &&). What am I doing wrong? Thanks in advance for any help!
df <- data.frame(
Gene = c("A","B","C","D","E"),
P_a = c(NA, NA, 21010, 14941,12),
E_a = c(NA, NA, "miss_b", "miss_b",NA),
P_b = c(1,200,32,NA,21),
E_b = c(NA, NA, "miss_a", NA,"miss_a"),
Eq = c("no", "yes", NA, NA,NA )
)
df$Sel <- ifelse(
(df$Eq == "no"), "same",
ifelse((df$Eq == "yes"), "diff",
ifelse (df$E_a == "miss_b" &
df$E_b == "miss_a", "G_P",
ifelse(is.na(df$P_b & df$E_b &
df$Eq),"in","out"
))))
This is the result_df that I would expect to generate with my code
df_result <- data.frame(
Gene = c("A","B","C","D","E"),
P_a = c(NA, NA, 21010, 14941,12),
E_a = c(NA, NA, "miss_b", "miss_b",NA),
P_b = c(1,200,32,NA,21),
E_b = c(NA, NA, "miss_a", NA,"miss_a"),
Eq = c("no", "yes", NA, NA, NA ),
Sel = c("same","diff", "G_P","in", "out")
)