简体   繁体   中英

R: data.table error

I\\m trying to run the following code:

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

I've used it before and it was perfectly fine but now I'm getting the error message:

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

I have loaded, library, uninstalled and re installed the package and nothing works...

Sample of my data, I do have many more columns and rows but essentially each row is an observation of a species with corresponding data

  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))

The 'df' is 'data.frame'. It needs to be converted to data.table Suppose if don't convert it, I get the error

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

By converting to data.table using setDT or as.data.table , it works

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM