0

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
zx8754
  • 52,746
  • 12
  • 114
  • 209
Sir Tee
  • 1
  • 2
  • Can you include your input data using `dput` as in `dput(my_data_frame)`. The function "." is likely because a library is not being included such as `plyr` (that is where I found it defined). – steveb Aug 10 '16 at 05:08
  • Done! Main post EDIT1. – Sir Tee Aug 10 '16 at 05:18
  • 1
    You only included a fragment from `dput`, that won't work for people trying to answer your question, as a minimal data set that reproduces the issue would help a lot. You need to find a minimal data set (say called `small_df`) that reproduces the issue, then include the results of `dput(small_df)`. That being said, you now have cleared up the original error and you have a different error. – steveb Aug 10 '16 at 05:35
  • Edited as instructed with small data set. Yes, the error is different! – Sir Tee Aug 10 '16 at 05:43
  • What is the line of code that produces the error? – steveb Aug 10 '16 at 06:00
  • The same one! `daily_averages<-weekly_cut[,.(average_viewers=mean(Viewers)),by=day_of_year]` – Sir Tee Aug 10 '16 at 06:04
  • So how does your actual minimal version of the rmarkdown file look like? Which package and R version(s) are you using? The issue resembles http://stackoverflow.com/questions/28513319/data-table-error-could-not-find-function – CL. Aug 10 '16 at 06:32

0 Answers0