Home > Net >  Different aimed chunks of code in a unique flow
Different aimed chunks of code in a unique flow

Time:10-10

I have the following code.

first chunk

data <- data %>% mutate(`Age (years)_cat` = case_when(`Age (years)`< 50 ~ 'young',
                                                      TRUE ~ 'old'))

second chunk

data %>%
  mutate(Death = ifelse(Death == 0, 'No Death', 'Death'))%$%
  table(Death, Treatment, `Age (years)_cat`) 

I have suited the code as follows since I needed to not execute the command in the second chunk on the first one. If I would write a unique dplyr code that respects such an issue, would it be somehow possible?

How should the code be set, in that case? If not are there some other altneratives? Thanks

CodePudding user response:

You could use parentheses:


(data <- data %>% mutate(`Age (years)_cat` = case_when(`Age (years)`< 50 ~ 'young',
                                                      TRUE ~ 'old'))) %>%
 mutate(Death = ifelse(Death == 0, 'No Death', 'Death')) %$%
 table(Death, Treatment, `Age (years)_cat`) 

Another option is to use assign:


data %>% mutate(`Age (years)_cat` = case_when(`Age (years)`< 50 ~ 'young',
                                                      TRUE ~ 'old')) %>%
 assign(x='data',value=.,.GlobalEnv) %>%
 mutate(Death = ifelse(Death == 0, 'No Death', 'Death'))%$%
 table(Death, Treatment, `Age (years)_cat`) 

  • Related