Create a column with case_when with ranges error appears

Asked

Viewed 33 times

-1

I have this database and I intend to create a new column with this structuring (see image). For example, if the row in the Calendar_week column has a value between 36 and 45 the new Quartile column is supposed to have the number "1".

datanew1$Quartile <- case_when(
  datanew1$Calendar_Week == seq(36,45) ~ "1",
  datanew1$Calendar_Week == seq(46,52)+seq(1,5) ~ "2",
  datanew1$Calendar_Week == seq(6,16) ~ "3",
  datanew1$Calendar_Week == seq(17,27) ~ "4"
)

The problem is that the code is giving me a mistake :

"1: In datanew1$Calendar_Week == seq(36, 45) :
  longer object length is not a multiple of shorter object length
2: In seq(46, 52) + seq(1, 5) :
  longer object length is not a multiple of shorter object length
3: In datanew1$Calendar_Week == seq(46, 52) + seq(1, 5) :
  longer object length is not a multiple of shorter object length
4: In datanew1$Calendar_Week == seq(6, 16) :
  longer object length is not a multiple of shorter object length
5: In datanew1$Calendar_Week == seq(17, 27) :
  longer object length is not a multiple of shorter object length

Any ideas on how to fix this? Thank you!

1 answer

0

In fact, I managed to solve the problem this way:

datanew1$Calendar_Week <- as.numeric(datanew1$Calendar_Week)
datanew1$Quartile <- case_when(
  datanew1$Calendar_Week %in% seq(36,45) ~ "1",
  datanew1$Calendar_Week %in% seq(46,52) ~ "2",
  datanew1$Calendar_Week %in% seq(6,16) ~ "3",
  datanew1$Calendar_Week %in% seq(17,27) ~ "4"
)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.