R使用knitr Rmarkdown和data.table时出错

时间:2016-08-10 05:05:06

标签: r data.table rstudio knitr r-markdown

我正在尝试对数据表进行子集化。下面的代码工作正常,没有打嗝(我已经简化了它,试图找出它不会编织的原因)。我有一个包含2列的数据表,

    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>

然后我得到一年中的某一天并将其转换为一个因子,并将时间列转换为包含星期几的因子:

    weekly_cut$day_of_year<-factor(format(weekly_cut$Time,"%j"))
    weekly_cut$Time<-factor(format(weekly_cut$Time,"%A"))

weekly_cut此时如下:

    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> 

然后我生成一个新的数据表,其中包含按年份平均的观看者:

    daily_averages<-weekly_cut[,.(average_viewers=mean(Viewers)),by=day_of_year]

一切正常,RStudio没有错误,但是当我编织时:

    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

请指教,谢谢!

EDIT1和2:

以下是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>)

我还在我的Rmd文档中添加了library(dplyr)library(plyr),错误更改为:

    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

0 个答案:

没有答案