I want to create a new variable. Depending on the expression of variable X, variable Y is to be divided by one of three values.
I tried tho following code, but it doesn't work and I don't know how to do it in another way...
df %>% mutate(new_var =
case_when(X == "yes" ~ df$Y / 2,
X == "no" ~ df$Y / 10,
X == "maybe" ~ df$Y / 12)
)
Glad for any help!
The code for my data set:
fixationtime <- fixationtime %>%
mutate(FIX_weighted = case_when(
outl_item == "outlier" ~ durFIX / 2,
outl_item == "no outlier" ~ durFIX / 10,
outl_item == "without outliers" ~ durFIX / 12
))
Subset of data for reproducing:
structure(list(outl_item = c("no outlier", "no outlier", "no outlier",
"outlier", "outlier", "outlier", "no outlier", "without outlier",
"without outlier", "no outlier", "no outlier", "outlier", "without outlier",
"without outlier", "outlier", "outlier", "outlier", "without outlier",
"without outlier", "no outlier", "outlier", "without outlier",
"without outlier", "without outlier", "no outlier", "outlier",
"without outlier", "no outlier", "without outlier", "without outlier"
), VP = structure(c(17L, 45L, 41L, 46L, 39L, 32L, 26L, 27L, 2L,
39L, 32L, 36L, 17L, 29L, 13L, 26L, 45L, 10L, 11L, 38L, 9L, 32L,
45L, 15L, 19L, 12L, 43L, 39L, 22L, 6L), levels = c("1", "2",
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",
"26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36",
"37", "38", "39", "40", "41", "42", "43", "44", "45", "46"), class = "factor"),
TRIAL = c(3L, 34L, 36L, 27L, 26L, 6L, 11L, 13L, 30L, 37L,
38L, 36L, 40L, 14L, 23L, 37L, 22L, 14L, 16L, 11L, 11L, 40L,
23L, 36L, 38L, 6L, 23L, 35L, 12L, 33L), BLOCK = c(7L, 1L,
3L, 8L, 7L, 6L, 4L, 7L, 2L, 5L, 2L, 2L, 3L, 1L, 7L, 4L, 3L,
8L, 3L, 6L, 3L, 2L, 3L, 1L, 1L, 8L, 1L, 1L, 6L, 6L), OUTL = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L), levels = c("no outlier",
"outlier"), class = "factor"), Condition = c("Parallel trials with outliers",
"Parallel trials with outliers", "Parallel trials with outliers",
"Parallel trials with outliers", "Parallel trials with outliers",
"Parallel trials with outliers", "Parallel trials with outliers",
"Parallel trials without outliers", "Parallel trials without outliers",
"Parallel trials with outliers", "Parallel trials with outliers",
"Parallel trials with outliers", "Parallel trials without outliers",
"Parallel trials without outliers", "Parallel trials with outliers",
"Parallel trials with outliers", "Parallel trials with outliers",
"Parallel trials without outliers", "Parallel trials without outliers",
"Parallel trials with outliers", "Parallel trials with outliers",
"Parallel trials without outliers", "Parallel trials without outliers",
"Parallel trials without outliers", "Parallel trials with outliers",
"Parallel trials with outliers", "Parallel trials without outliers",
"Parallel trials with outliers", "Parallel trials without outliers",
"Parallel trials without outliers"), durFIX = c(1515L, 1657L,
1315L, 0L, 175L, 762L, 946L, 1780L, 1417L, 1719L, 1686L,
576L, 1711L, 1559L, 0L, 0L, 586L, 1792L, 1708L, 1532L, 624L,
1685L, 1717L, 1386L, 1426L, 227L, 1688L, 1581L, 1042L, 1345L
)), row.names = c(NA, -30L), class = "data.frame")