Home > Back-end >  How to combine these lines into one command in R
How to combine these lines into one command in R

Time:12-24

I want to run these as one line and combine them.

df.lami$outcome.back <- df.lami$outcome.back %>% 
  replace(between(df.lami$outcome.back,3,10),"much improved")

df.lami$outcome.back <- df.lami$outcome.back %>% 
  replace(between(df.lami$outcome.back,2,2.9),"improved")

CodePudding user response:

You may try using dplyr::case_when

library(dplyr)
df.lami %>%
  mutate(outcome.back = case_when(
    3 <= outcome.back& outcome.back <= 10 ~ "much improved",
    2 <= outcome.back& outcome.back <= 2.9 ~ "improved",
    T ~ NA_character_ # up to you
  ))

CodePudding user response:

You may use cut for this. Also it is easier to help when you provide a small example of your data.

df <- data.frame(a = c(2.9, 3.2, 8.9, 5))
df$b <- cut(df$a, c(2, 3, 10), c('improved', 'much improved'))
df

#    a             b
#1 2.9      improved
#2 3.2 much improved
#3 8.9 much improved
#4 5.0 much improved
  •  Tags:  
  • r
  • Related