Reputation: 1683
I have written a wrapper function to format dates within a data frame to either a Monday or a Sunday using lubridate's floor_date function, and am receiving a rather opaque error message.
The function looks like:
week_format<-function(data,date,day){
if(typeof(data)!="list") { stop("Error, data should be a data.frame")}
ifelse(day=="MON",day<-1,day<-7)
data$date<- floor_date( data$date - days(1),"week") + days(day)
return(data)
}
When I attempt to use the function :
week_format(dataset,wc_Monday,"MON")
I get error:
Error in slot(object, name) :
cannot get a slot ("call") from an object of type "double"
I am unsure what this error means, and cannot recreate it outside the function :
data$date<- floor_date( data$date - days(1),"week") + days(7)
Works without error.
I should add data$date is already formatted as a date in R.
Here is a dput of the data:
structure(list(X.8 = 1:178, wc_Monday = structure(c(15341, 15348,
15355, 15362, 15369, 15376, 15383, 15390, 15397, 15404, 15411,
15418, 15425, 15432, 15439, 15446, 15453, 15460, 15467, 15474,
15481, 15488, 15495, 15502, 15509, 15516, 15523, 15530, 15537,
15544, 15551, 15558, 15565, 15572, 15579, 15586, 15593, 15600,
15607, 15614, 15621, 15628, 15635, 15642, 15649, 15656, 15663,
15670, 15677, 15684, 15691, 15698, 15705, 15712, 15719, 15726,
15733, 15740, 15747, 15754, 15761, 15768, 15775, 15782, 15789,
15796, 15803, 15810, 15817, 15824, 15831, 15838, 15845, 15852,
15859, 15866, 15873, 15880, 15887, 15894, 15901, 15908, 15915,
15922, 15929, 15936, 15943, 15950, 15957, 15964, 15971, 15978,
15985, 15992, 15999, 16006, 16013, 16020, 16027, 16034, 16041,
16048, 16055, 16062, 16069, 16076, 16083, 16090, 16097, 16104,
16111, 16118, 16125, 16132, 16139, 16146, 16153, 16160, 16167,
16174, 16181, 16188, 16195, 16202, 16209, 16216, 16223, 16230,
16237, 16244, 16251, 16258, 16265, 16272, 16279, 16286, 16293,
16300, 16307, 16314, 16321, 16328, 16335, 16342, 16349, 16356,
16363, 16370, 16377, 16384, 16391, 16398, 16405, 16412, 16419,
16426, 16433, 16440, 16447, 16454, 16461, 16468, 16475, 16482,
16489, 16496, 16503, 16510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L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("X.8",
"wc_Monday", "Payday", "Friday_Payday", "Good_Friday"), row.names = c(NA,
-178L), class = "data.frame")
Upvotes: 1
Views: 162
Reputation: 20389
Ok, you should change your code to
week_format<-function(mdat, mslot, day){
if(typeof(mdat)!="list") {
stop("Error, data should be a data.frame")
}
day <- if (day == "MON") 1 else 7
mdat[[mslot]] <- floor_date(mdat[[mslot]] - days(1), "week") + days(day)
return(data)
}
And then you should call your function like this
week_format(data, "wc_Monday", "MON")
Upvotes: 1
Reputation: 1683
So I have solved this by changing the indexation :
However I am unsure as to why this would fix the error if anyone could expand on the differences between $
and [,]
I would be grateful.
week_format<-function(data,date,day){
if(typeof(data)!="list") { stop("Error, data should be a data.frame")}
ifelse(day=="MON",day<-1,day<-7)
data[,date]<- floor_date( data[,date] - days(1),"week") + days(day)
return(data)
}
Upvotes: 1