R:data.table错误

时间:2015-04-16 05:17:45

标签: r data.table

我正在尝试运行以下代码:

df[,{
r10s     <- 1:.N/.N < .1
myrows   <- if(sum(r10s)>0){r10s}else{TRUE}
c(
    .SD[myrows],
    list(first10=mean(day[r10s]))
)
 },.(species,year)]  

我之前使用过它并且完全没问题,但现在我收到了错误消息:

[.data.frame中的错误(数据,{:找不到功能“。”

我已经加载,库,卸载并重新安装了包,没有任何作用......

我的数据示例,我确实有更多的列和行,但基本上每行都是对具有相应数据的物种的观察

  df=data.frame(
  year=c(1901,1901,1901,1901,1901,1901,1901,1901,1901,1901,1901,1901,1901),
  temp=c(29,25,21,26,20,20,26,25,24,23,23,24,26),
  habitat=c("fst","fld","city","city","fst","fld","fst","road","river","river","city","city","city"),
  species=c("blu","blu","pink","pink","pink","pink","pink","pink","pink","pink","pink","pink","pink"),
  day= c(34,87,93,79,56,98,100,187,54,14,63,57,23))

1 个答案:

答案 0 :(得分:4)

&#39; df&#39;是数据框架&#39;。它需要转换为data.table假设如果不进行转换,我会收到错误

Error in `[.data.frame`(df, , { : could not find function "."

使用data.tablesetDT转换为as.data.table,可行

setDT(df)[,{
  r10s     <- 1:.N/.N < .1
  myrows   <- if(sum(r10s)>0){r10s}else{TRUE}
c(
   .SD[myrows],
  list(first10=mean(day[r10s]))
)
},.(species,year)]  
#   species year temp habitat day first10
#1:     blu 1901   29     fst  34     NaN
#2:     blu 1901   25     fld  87     NaN
#3:    pink 1901   21    city  93      93