vendredi 1 octobre 2021

If, then statements in R

I am teaching myself R and trying to figure out how to properly write an if, then statement. I have a df of demographic data and I want to create a variable for highest degree in the household, based on marital status. If the data is coded a 0, 3, or 7, it is a single-parent (mom) household and then would use the mother's degree. But, if it's 1, 2, 4, 5, or 6, I want to use the highest of either caregiver's education.

> ExcelData <-df
> marital <- c(1, 0, 1, 5, 7, 2, NA, 1)
> education <- c("10", "12", "13", "14", "16", "18", "12", "14")
> education_partner <- c("11", "18", "10", "14", "11", "12", "16", "16")

i wrote this and it works, creating a new df with maternal education, but I'm not sure how to write it where it prints the greater value of education or education_partner when it is a 1, 2, 4, 5, or 6. any advice is greatly appreciated!!

if(ExcelData$marital =  0 | 3:7 )
  hhHighestDegree <- (ExcelData$education)

Aucun commentaire:

Enregistrer un commentaire