Error in mutate(), rbind, and seq.int

The following code was working earlier, so I am not sure what went wrong! I was referring to this link to do something very similar [survival analysis - R Optimal way to create time series from start and end dates for groups - Stack Overflow]

The error occurs after running these 3 lines:

csuite <- csuite %>%
mutate(DateStartRole=ifelse(DateStartRole=="N", NA, DateStartRole)) %>%
mutate(DateStartRole = as.Date(DateStartRole, "%Y%m%d"))

This is the error:
Error: Problem with mutate() input DateStartRole.
x character string is not in a standard unambiguous format
:information_source: Input DateStartRole is ifelse(DateStartRole == "N", NA, DateStartRole).

I am also having the following two errors:

Here is the code:
range <- rbindlist(lapply(csuite(csuite$DateStartRole, csuite$DateEndRole, csuite$CompanyID),as.data.frame))

Here is the error:
Error in csuite(csuite$DateStartRole, csuite$DateEndRole, csuite$CompanyID) :
could not find function "csuite"

Here is the code:
csuite <- data.table(csuite)
csuite[,:=("DateStartRole" = as.Date(DateStartRole), "DateEndRole" = as.Date(DateEndRole))]
csuite[,num_mons:= length(seq(from=DateStartRole, to=DateEndRole, by='month')),by=1:nrow(csuite)]

Here is the Error
Error in seq.int(r1$mon, 12 * (to0$year - r1$year) + to0$mon, by) :
'from' must be a finite number

perhaps use NA_character here instead of NA?

As csuite is not a function it is an error to open brackets after it and pass it parameters

I should have mentioned that csuite is the name of my dataset.

I had guessed as much when I drew your attention to your mistake in using it as though it were a function (which it is not)

It worked better to use 20000101 instead of "NA" in date start role. Any chance you are able to assist with the other two issues? Here is a sample data set:

csuite<-structure(list(id = c(723654, 885618, 269861, 1383642, 250276,
815511, 1506680, 1567855, 667345, 795731), startdate = c("20080629",
"20081201", "20060927", "20100203", "20060831", "20080910",
"20100411", "20100515", "20080412", "20080828"), enddate = c("20080813",
"20090208", "20071012", "20100909", "20070630", "20100427",
"20100413", "20100516", "20100420", "20100309")), .Names = c("id",
"DateStartRole", "DateEndRole"), class = "data.frame", row.names = c("1",
"2", "3", "4", "6", "7", "8", "9", "10", "11"))

With the first error, the function doesn't do anything so I don't know how you could have had that error. There are no values in the DateStartRole column that are "N".

tibble::tribble(
                 ~id, ~DateStartRole, ~DateEndRole,
              723654,     "20080629",   "20080813",
              885618,     "20081201",   "20090208",
              269861,     "20060927",   "20071012",
             1383642,     "20100203",   "20100909",
              250276,     "20060831",   "20070630",
              815511,     "20080910",   "20100427",
             1506680,     "20100411",   "20100413",
             1567855,     "20100515",   "20100516",
              667345,     "20080412",   "20100420",
              795731,     "20080828",   "20100309"
             )

For the other error

You didn't provide any data with CompanyID.

Also, you should note the packages that you are using, e.g.

library(dplyr)
library(data.table)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.