Error: Not compatible with requested type: [type=character; target=double].

I am currently getting the above error and can't figure out why. My code worked fine yesterday but all of a sudden is now throwing this error when it gets to the part about adding in the weeks.


data1<-getURL("https://raw.githubusercontent.com/ST47S-CompStats-Fall2019/team-shows/master/cleaned_data/Bachelor_2018.csv?token=ALH3QRX4NN6HGS5GAWXCL5S6APD6W")
bachelor_2018<-read.csv(text=data1)

#make the original date format into Y-M-D

bachelor_2018<-separate(bachelor_2018, date, into = c("day", "month", "date", "time1", "time2", "year"), sep = " ", remove = TRUE, convert = FALSE, extra = "warn", fill = "warn")
#covert the months to numbers
bachelor_2018$month[which(bachelor_2018$month == "Dec")] = "12"
bachelor_2018$month[which(bachelor_2018$month == "Jan")] = "01"
bachelor_2018$month[which(bachelor_2018$month == "Feb")] = "02"
bachelor_2018$month[which(bachelor_2018$month == "Mar")] = "03"
#select the relevant variables for the new data set
bachelor_2018 <- bachelor_2018 %>%
  select(month, date, year, polarity, state, text)
#y-m-d format
form.dates <- ymd(paste(bachelor_2018$year, bachelor_2018$month, bachelor_2018$date, sep = "-"))
#eliminate previous month and year column
#overwrite dates column with new dates
bachelor_2018$date<-form.dates
bachelor_2018$month<-NULL
bachelor_2018$year<-NULL


#create and assign a weeks column based on dates taken from wikipedia
bachelor_2018<-bachelor_2018 %>%
  mutate(Weeks = case_when(
    between(date, "2017-12-09", "2017-12-31") ~ "pre-show",
    between(date, "2018-01-01", "2018-01-07") ~ "week 1",
    between(date, "2018-01-08", "2018-01-14") ~ "week 2",
    between(date, "2018-01-15", "2018-01-21") ~ "week 3",
    between(date, "2018-01-22", "2018-01-28") ~ "week 4",
    between(date, "2018-01-29", "2018-02-04") ~ "week 5",
    between(date, "2018-02-05", "2018-02-11") ~ "week 6",
    between(date, "2018-02-12", "2018-02-18") ~ "week 7",
    between(date, "2018-02-19", "2018-02-25") ~ "week 8",
    between(date, "2018-02-26", "2018-03-04") ~ "week 9",
    between(date, "2018-03-05", "2018-03-14") ~ "week 10"
  ))

#create a variable that assigns the season to each row
bachelor_2018<-bachelor_2018 %>%
  mutate(Season = "Bachelor 2018")

#create a csv of the new, altered data
write.csv(bachelor_2018, file = "bachelor_2018.csv")



Any idea what's going wrong?

The between() function expects numeric values. Try converting the strings into dates.

bachelor_2018<-bachelor_2018 %>%
  mutate(Weeks = case_when(
    between(date, as.Date("2017-12-09"), as.Date("2017-12-31")) ~ "pre-show",
    between(date, as.Date("2018-01-01"), as.Date("2018-01-07")) ~ "week 1",
    between(date, as.Date("2017-12-09"), as.Date("2017-12-31")) ~ "pre-show",
    between(date, as.Date("2018-01-01"), as.Date("2018-01-07")) ~ "week 1",
    between(date, as.Date("2018-01-08"), as.Date("2018-01-14")) ~ "week 2",
    between(date, as.Date("2018-01-15"), as.Date("2018-01-21")) ~ "week 3",
    between(date, as.Date("2018-01-22"), as.Date("2018-01-28")) ~ "week 4",
    between(date, as.Date("2018-01-29"), as.Date("2018-02-04")) ~ "week 5",
    between(date, as.Date("2018-02-05"), as.Date("2018-02-11")) ~ "week 6",
    between(date, as.Date("2018-02-12"), as.Date("2018-02-18")) ~ "week 7",
    between(date, as.Date("2018-02-19"), as.Date("2018-02-25")) ~ "week 8",
    between(date, as.Date("2018-02-26"), as.Date("2018-03-04")) ~ "week 9",
    between(date, as.Date("2018-03-05"), as.Date("2018-03-14")) ~ "week 10"
  ))

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