vendredi 26 mai 2017

adding a column based on values of another columns

I have the following data:

> dput(head(sub1, 5))
structure(list(SchadensummeOhneZins = c(35058, 3764, 5022, 2071, 
9306), Baujahr = c(1981L, 1860L, 1847L, 1900L, 1905L), Zweck = c(202L, 
280L, 202L, 200L, 200L), VolumenM3 = c(6028L, 1812L, 1513L, 3404L, 
669L), intHQ30 = structure(c(NA, NA, 1L, 2L, NA), .Label = c("1", 
"2", "3"), class = "factor"), intHQ100 = structure(c(1L, 1L, 
1L, 2L, 1L), .Label = c("1", "2", "3"), class = "factor"), intHQ300 = structure(c(1L, 
1L, 1L, 2L, 1L), .Label = c("1", "2", "3"), class = "factor"), 
    Jaehrlichkeit = c("HQ100", "HQ100", "HQ100", "HQ100", "HQ100"
    )), .Names = c("SchadensummeOhneZins", "Baujahr", "Zweck", 
"VolumenM3", "intHQ30", "intHQ100", "intHQ300", "Jaehrlichkeit"
), row.names = c(1113L, 1132L, 1134L, 1136L, 1137L), class = "data.frame")

The variable Jaehrlichkeit is basically a factor with 3 levels: HQ30, HQ100, HQ300. I want R to read Jaehrlichkeit. If Jaehrlichkeit = HQ30, the copy the value from the column intHQ30 in the correponding row and paste it in the newly created column Intensitaet.

I was trying to combine the mutate function with nested ifelse but keep getting errors. Can please someone help me out? or maybe suggest an easier solution?

Thank you:

Aucun commentaire:

Enregistrer un commentaire