0

I need to create a variable that characterizes individuals as been "increased", "decreased" or "no change" using the number values in 2 columns. I've tried but I was only able to output the change. How do I include "no change" into the subset and add labels to the values. For example if time1 > time 2 I want R to output "increase" or "decrease" if time1 < time2 and no change if the values don't change. This is what I've done so far but I'm a bit lost.

change<-c(ifelse(a1$time1 > a1$time2, "increase", "decrease" | a1$time==a1$time2, "no change")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • `change <- ifelse(a1$time1 > a1$time2, "increase", ifelse(a1$time==a1$time2, "no change", "decrease"))` . Also look into `?dplyr::case_when` which is an alternative to nested `ifelse`. – Ronak Shah Mar 05 '22 at 01:08
  • With regard to the `case_when` option from the `dplyr` package: `df %>% mutate(change = case_when(before == after ~ "no change", before < after ~ "increased", before > after ~ "decreased"))` – rdelrossi Mar 05 '22 at 01:15

0 Answers0