I have a data frame of multiple columns. I want to create a new column called second.indicator
with the following :
second.indicator == -1 if il.count.description + un.count.description > elle.count.description + une.count.description
second.indicator == 0 if il.count.description + un.count.description) == elle.count.description + une.count.description
else second.indicator == 1
The output is many error messages:
'second.indicator' not found / Error: unexpected '}' in " }"
il.count.description elle.count.description un.count.description un.count.description
5 1 5 1
9 2 2 6
1 1 0 0
10 9 0 8
data <- data %>%
mutate(second.indicator =
if (il.count.description + un.count.description) > elle.count.description + une.count.description) {
second.indicator == -1
} else if (il.count.description + un.count.description) == (elle.count.description + une.count.description) {
second.indicator == 0
} else {
second.indicator == 1
}
You cannot use if
-statements inside mutate
like that (for multiple reasons); instead you could use case_when
:
data <-
data %>%
mutate(second.indicator = case_when((il.count.description + un.count.description) > (elle.count.description + une.count.description) ~ -1,
(il.count.description + un.count.description) == (elle.count.description + une.count.description) ~ 0,
T ~ 1
)