I would like to change the levels of the Death variable in my dataset (and I have done this as follows):
data <- data %>%
mutate(Death = ifelse(Death == 0, 'No Death', 'Death'))
and then to summarise it as follows:
sum_table <- data %>%
group_by(Treatment, Death) %>%
summarize(n = n()) %>%
mutate(freq = n/sum(n)) %>%
print()
This is the only way I know to realize both things. If I wrapped them together as follows:
data %>%
mutate(Death = ifelse(Death == 0, 'No Death', 'Death'))%>%
group_by(Treatment, Death) %>%
summarize(n = n()) %>%
mutate(freq = n/sum(n)) %>%
print()
the variable Death is still coded as 1 and 0 into the original dataset.
Could you possibly suggest the term that is lacking to run the code altogether and save the new levels of that variable in the dataset?
Thanks
CodePudding user response:
We may use the %<>%
operator from magrittr
to make the changes in the original data
library(magrittr)
library(dplyr)
data %<>%
mutate(Death = ifelse(Death == 0, 'No Death', 'Death')) %>%
group_by(Treatment, Death) %>%
summarize(n = n()) %>%
mutate(freq = n/sum(n))