I am new to R but am excited to learn it and I thought this might be a good opportunity. I have two measurements of salinity (uS and mS.m_1.5). I have created 3 classes (1, 2, 3) for each measurement type (uSClass and mS.m_1.5Class) based on their values. For many of the observations, I only have 1 measurement type. I want to create a new class (SClass) based on these two classes.
Any observation of uSClass = 1 and mS.m_1.5Class = 1, should be SClass 1.
Any observation of uSClass = 1 and mS.m_1.5Class = NA, should be SClass 1.
Any observation of uSClass = NA and mS.m_1.5Class = 1, should be SClass 1. etc...
Any observation with conflicting classes (ex. uSClass = 1 and mS.m_1.5Class = 2) should not be assigned a class (NA). This is my code:
std$SClass <- ifelse(std$uSClass == 1 & std$mS.m_1.5Class == 1, 1,
ifelse(std$uSClass == 1 & is.na(std$mS.m_1.5Class), 1,
ifelse(is.na(std$uSClass) & std$mS.m_1.5Class == 1, 1,
ifelse(std$uSClass == 2 & std$mS.m_1.5Class == 2, 2,
ifelse(std$uSClass == 2 & is.na(std$mS.m_1.5Class), 2,
ifelse(is.na(std$uSClass) & std$mS.m_1.5Class == 2, 2,
ifelse(std$uSClass == 3 & std$mS.m_1.5Class == 3, 3,
ifelse(std$uSClass == 3 & is.na(std$mS.m_1.5Class), 3,
ifelse(is.na(std$uSClass) & std$mS.m_1.5Class == 3, 3, NA)))))))))
It makes logical sense to me but it must not be correct. The only classifications that work are those where both uSClass and mS.m_1.5Class have values. If I run the entire code, most observations are assigned NA. I have tried a couple other methods incorporating | operators but those have not worked either. Your help is appreciated!