mercredi 3 novembre 2021

R ifelse statement ignores input

I have a problem making an ifelse / if_else statement. I want to create a new column "Transtyp" which is assigned a "2" if the column "Plot" has an uneven number (1 or 3 or 5 or 7) and is assigned a "5" when "Plot" has an even number. This is my df

df <- structure(list(Soll = c("1189", "1189", "1189", "1189", "1189", 
"1189", "1189", "1189", "1189", "1189", "1189", "1189", "1189", 
"1189", "1189", "1189", "1189", "1189", "1189", "1189"), Datum = c("2021-03-15", 
"2021-03-15", "2021-03-15", "2021-03-15", "2021-03-15", "2021-03-16", 
"2021-03-16", "2021-03-16", "2021-03-16", "2021-03-16", "2021-03-17", 
"2021-03-17", "2021-03-17", "2021-03-17", "2021-03-17", "2021-03-18", 
"2021-03-18", "2021-03-18", "2021-03-18", "2021-03-18"), Plot = c(1, 
4, 6, 7, 8, 1, 4, 6, 7, 8, 1, 4, 6, 7, 8, 1, 4, 6, 7, 8), Temp = c(21.4609852941176, 
21.5064705882353, 21.4601323529412, 21.4587352941176, 21.4630147058824, 
11.0828229166667, 11.03209375, 11.3093020833333, 11.2860833333333, 
11.1405104166667, 2.1683125, 2.10675, 2.87871875, 2.84852083333333, 
2.44628125, 1.41326041666667, 1.47109375, 1.66685416666667, 1.53930208333333, 
1.34465625), RH = c(37.8671617647059, 37.8925, 38.0004411764706, 
38.26425, 37.9111029411765, 62.1298645833333, 63.2143333333333, 
62.1141666666667, 62.55275, 62.5445416666667, 92.7891041666667, 
93.5631458333333, 91.8378333333333, 93.2626145833333, 92.565625, 
92.7335625, 93.5223125, 92.7023541666667, 93.6509375, 93.120375
), Transtyp = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2)), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

Now, I am using this code, but it only assigns "2" if Plot == 1 and ignores the rest. What am I missing?

df %>% mutate(Plot = as.numeric(Plot)) %>%
        mutate(Transtyp = ifelse(Plot == (1 | 3 | 5 | 7), "2", "5")) -> df_t

Any help is appreciated! Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire