I'm new to R. I'm trying to set a new column in my data frame depending on what's in 3 other columns. I've looked at other queries like:
Populate a column using if statements in r
Which I thought would solve it but it looks like I can only give sapply a single vector as when I try the following code:
IHC <- c("N","N","Y","N","N")
CCD <- c("13-Nov-2009", NA, "09-Feb-2011", "10-Dec-2012", "16-Nov-2009")
IHE <- c(NA, "20-Feb-2011",NA,NA,NA)
df1 <- data.frame(IHC, CCD, IHE)
InHouse <- function(IHC,CCD,IHE) {
if(IHE == "" && CCD == NA | IHC == "N") y <- ""
if(IHE == "") y <- CCD
if(CCD > IHE) y <- IHE
else y <- CCD
return(y)
}
df1$AAA <- sapply(c(df1$IHC, df1$CCD, df1$IHE), InHouse)
I get the following error:
Error in IHE == "" : 'IHE' is missing
Any help would be great.