Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to change variable levels in dataser, save the new level created and go further with a dplyr code

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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

>Solution :

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)) 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading