I am trying to subset a data table. The code below works fine with no hiccups (I've simplified it to try to figure out why it won't knit). I have a data table with 2 columns,
Classes ‘data.table’ and 'data.frame': 7388 obs. of 2 variables:
$ Time : POSIXct, format: "2016-07-05 16:10:18" "2016-07-05 16:12:15" "2016-07-05 16:16:27" ...
$ Viewers: int 5923 6006 6109 5925 5708 5829 5989 6007 5963 5980 ...
- attr(*, ".internal.selfref")=<externalptr>
Then I get the day of year and turn it into a factor, and convert the Time column into a factor containing day of the week:
weekly_cut$day_of_year<-factor(format(weekly_cut$Time,"%j"))
weekly_cut$Time<-factor(format(weekly_cut$Time,"%A"))
weekly_cut
at this point is as follows:
Classes ‘data.table’ and 'data.frame': 7388 obs. of 3 variables:
$ Time : Factor w/ 7 levels "Friday","Monday",..: 6 6 6 6 6 6 6 6 6 6 ...
$ Viewers : int 5923 6006 6109 5925 5708 5829 5989 6007 5963 5980 ...
$ day_of_year: Factor w/ 27 levels "187","188","189",..: 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, ".internal.selfref")=<externalptr>
Then I generate a new datatable with viewers averaged by day of year:
daily_averages<-weekly_cut[,.(average_viewers=mean(Viewers)),by=day_of_year]
Everything works fine, RStudio throws no errors, but when I knit:
Quitting from lines 330-346 (index.Rmd)
Error in `[.xts`(weekly_cut, , .(average_viewers = mean(Viewers)), by = day_of_year) :
could not find function "."
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> [ -> [.xts
Execution halted
Please advise, thank you!
EDIT1&2:
Here is the output from dput(weekly_cut_small)
:
structure(list(Time = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L), .Label = c("Friday", "Monday", "Saturday", "Sunday",
"Thursday", "Tuesday", "Wednesday"), class = "factor"), Viewers = c(5923L,
6006L, 6109L, 5925L, 5708L, 5829L, 5989L, 6007L, 5963L, 5980L
), day_of_year = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("187", "188", "189", "190", "191", "192",
"193", "194", "195", "196", "197", "198", "199", "200", "201",
"202", "203", "204", "205", "206", "207", "208", "209", "210",
"211", "212", "213"), class = "factor")), .Names = c("Time",
"Viewers", "day_of_year"), class = c("data.table", "data.frame"
), row.names = c(NA, -10L), .internal.selfref = <pointer: 0x0000000000340788>)
I have also put library(dplyr)
and library(plyr)
in my Rmd document and the error changes to:
Quitting from lines 330-346 (index.Rmd)
Error in which(!as.logical(j)) :
(list) object cannot be coerced to type 'logical'
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> [ -> [.xts -> which
Execution halted