0

Why do I get "warning longer object length is not a multiple of shorter object length"?

Forgive me for asking this again, but I am unable to figure out why I am getting this error message - even after combing through stackoverflow. From the above link it says:

"memb only has a length of 10. I'm guessing the length of dih_y2$MemberID isn't a multiple of 10. When using == it will spit out a warning if it isn't a multiple to let you know that it's probably not doing what you're expecting it is doing."

I'm am getting the same error message from the following code, but I am not sure what "objects" are of different length in my example and how to fix it! Essentially, I am trying to separate my dates into months for analysis. Please help if you can. Thank you.

library(ggplot2)
library(dplyr)
library(statsr)

piccolos2 <- piccolos2 %>%
  mutate(SERPDate = as.Date(piccolosRankings$SERPDate, format='%m/%d/%Y'))

piccolos2 <- piccolos2 %>%
  mutate(Month = ifelse(as.numeric(SERPDate) %in% 0017-04-01:0017-04-30, "April",
             ifelse(as.numeric(SERPDate) %in% 0017-05-01:0017-05-31, "May",
             ifelse(as.numeric(SERPDate) %in% 0017-06-01:0017-06-30, "June",
             ifelse(as.numeric(SERPDate) %in% 0017-07-01:0017-07-31, "July", "August")))))  
Axeman
  • 32,068
  • 8
  • 81
  • 94
min7b5_b9_b11_b13
  • 147
  • 1
  • 1
  • 8
  • 1
    Are you aware that `0017-04-01:0017-04-30` is not a sequence of dates? And the same for the others. Try it at a command line. – Rui Barradas Aug 12 '17 at 18:49
  • 1
    In the expression `0017-04-01:0017-04-30` the operator `:` takes precedence. Then there are the subtractions. All in all it evaluates to the sequence `[1] -22 -23 -24 -25 -26 -27 -28 -29 -30 -31 -32 -33 -34 -35 -36 -37 -38`. To make dates of it you must quote the expressions on each side of the `:` and then use `as.Date`, `as.POSIXct` or maybe package `lubridate`. – Rui Barradas Aug 12 '17 at 19:01
  • Use `case_when` instead of embedded `ifelse` – moodymudskipper Aug 12 '17 at 19:03
  • @RuiBarradas Ok, could you check the above - I added how I took care of the dates, and they went from 4/6/17 to 0017-04-06. However I did do piccolos2$SERPDate and I got -713223. How does that relate to the date? – min7b5_b9_b11_b13 Aug 12 '17 at 19:05
  • @RuiBarradas I think I see what you mean. Let me try that. – min7b5_b9_b11_b13 Aug 12 '17 at 19:08
  • @RuiBarradas It worked. I'll post the answer in a second. But let me ask you this: so I put the quotations around the "numbers" because they weren't actually numbers? And the as.Date turned it into what exactly? Thanks again – min7b5_b9_b11_b13 Aug 12 '17 at 19:14
  • Please read `help(strptime)`. All your questions will be answered :) – Rich Scriven Aug 12 '17 at 19:26
  • Thanks @RichScriven – min7b5_b9_b11_b13 Aug 12 '17 at 19:27
  • @RichScriven already answered the question, but I'll add that `as.Date` turned them into objects of class `Date`. Run `as.Date("2010-01-01"):as.Date("2010-01-31")` and `seq(as.Date("2010-01-01"), as.Date("2010-01-31"), by = "day")` and see the difference. Note that in the second case you are calling the method for class `Date`, i.e. function `seq.Date`. – Rui Barradas Aug 13 '17 at 18:18

1 Answers1

0
piccolos2 <- piccolos2 %>%
  mutate(Month = ifelse(as.numeric(SERPDate) %in% as.Date("0017-04-01"):as.Date("0017-04-30"), "April",
             ifelse(as.numeric(SERPDate) %in% as.Date("0017-05-01"):as.Date("0017-05-31"), "May",
             ifelse(as.numeric(SERPDate) %in% as.Date("0017-06-01"):as.Date("0017-06-30"), "June",
             ifelse(as.numeric(SERPDate) %in% as.Date("0017-07-01"):as.Date("0017-07-31"), "July", "August")))))  
min7b5_b9_b11_b13
  • 147
  • 1
  • 1
  • 8