Keep value if not in dplyr::case_when statement

In case_when all values which do not pass one of the given conditions will be set to an NA in the new column.
I would like to keep them as they are, if the other conditions are FLASE.

In this former post I found the tip to set the last statement to:


but it doesn´t work in my code below:

rsquared <- stroke_angel_data_grouped %>%
  select(ly_rd4iss, ly_nihssbauf) %>%
  na.omit() %>%
  do(glance(lm(ly_nihssbauf ~ ly_rd4iss, data = .))) %>% 
  select(r.squared, p.value) %>% 
  # assign a text to p values in a new row for better readability in the table
  mutate(p.values.text = case_when(p.value <= 0.001 ~ "P < 0.001",
                                   p.value <= 0.01 ~ "P < 0.01",
                                   p.value <= 0.05 ~ "P < 0.05",
                                   TRUE ~ as.numeric(as.character(p.value)))) %>% 

does anyone notice my mistake?

The result should be character as on the right side of case when you started setting it to character strings. Therefore remove the as.numeric part

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.