data.table一次计算几列

时间:2014-12-15 23:09:21

标签: r data.table

提前感谢您阅读本文。我有一个在data.table 1.9.3上工作正常的函数。但今天我更新了我的data.table包,我的功能不起作用。

这是我在data.table 1.9.3上的函数和工作示例:

trait.by <- function(data,traits="",cross.by){
  traits = intersect(traits,names(data))
  if(length(traits)<1){  
    #if there is no intersect between names and traits
    return(      data[,       list(N. = .N),    by=cross.by])
  }else{
    return(data[,c(   N. = .N,
                    MEAN = lapply(.SD,function(x){return(round(mean(x,na.rm=T),digits=1))}) , 
                    SD   = lapply(.SD,function(x){return(round(sd  (x,na.rm=T),digits=2))}) ,
                    'NA' = lapply(.SD,function(x){return(sum  (is.na(x)))})),
                 by=cross.by, .SDcols = traits])
  }
}

> trait.by(data.table(iris),traits = c("Sepal.Length",    "Sepal.Width"),cross.by="Species")
#      Species N. MEAN.Sepal.Length MEAN.Sepal.Width SD.Sepal.Length
#1:     setosa 50               5.0              3.4            0.35
#2: versicolor 50               5.9              2.8            0.52
#3:  virginica 50               6.6              3.0            0.64
#   SD.Sepal.Width NA.Sepal.Length NA.Sepal.Width
#1:           0.38               0              0
#2:           0.31               0              0
#3:           0.32               0              0

我在MEAN.(traits)变量中为所有列计算SD.(traits)NA.(traits)traits


当我使用data.table 1.9.4运行时,我收到以下错误:

> trait.by(data.table(iris),traits = c("Sepal.Length",    "Sepal.Width"),cross.by="Species")
#Error in assign("..FUN", eval(fun, SDenv, SDenv), SDenv) : 
#  cannot change value of locked binding for '..FUN'

知道如何解决这个问题吗?!

2 个答案:

答案 0 :(得分:4)

更新:现在1.9.5 commit 1680已解决此问题。来自NEWS

  
      
  1. 修正了j-expression内部优化中的错误,其中包含多个lapply(.SD, function(..) ..),如图所示here on SO。关闭#985。感谢@jadaliha的报告和@BrodieG的SO调试。
  2.   

现在这可以按预期工作:

data[,
  c(
    MEAN = lapply(.SD,function(x){return(round(mean(x,na.rm=T),digits=1))}),
    SD = lapply(.SD,function(x){return(round(sd  (x,na.rm=T),digits=2))})
  ), by=cross.by, .SDcols = traits]    

这看起来像一个错误,表现为lapply(.SD, FUN)在一次data.table调用中与c(结合使用c(。您可以将.(替换为traits <- c("Sepal.Length", "Sepal.Width") cross.by <- "Species" data <- data.table(iris) data[, c( MEAN = lapply(.SD,function(x){return(round(mean(x,na.rm=T),digits=1))}) ), by=cross.by, .SDcols = traits ] 来解决此问题。

data[,
  c(
    SD = lapply(.SD,function(x){return(round(sd  (x,na.rm=T),digits=2))})
  ),
  by=cross.by, .SDcols = traits
]

作品。

data[,
  c(
    MEAN = lapply(.SD,function(x){return(round(mean(x,na.rm=T),digits=1))}),
    SD = lapply(.SD,function(x){return(round(sd  (x,na.rm=T),digits=2))})
  ),
  by=cross.by, .SDcols = traits
]    

作品。

data[,
  .(
    MEAN = lapply(.SD,function(x){return(round(mean(x,na.rm=T),digits=1))}),
    SD = lapply(.SD,function(x){return(round(sd  (x,na.rm=T),digits=2))})
  ),
  by=cross.by, .SDcols = traits
]

不能工作

{{1}}

作品。

答案 1 :(得分:2)

喜欢这个?输出格式略有改变。但结果就在那里。

trait.by <- function(data,traits="",cross.by){
  traits = intersect(traits,names(data))
  if(length(traits)<1){  
    #if there is no intersect between names and traits
    return(data[, list(N. = .N), by=cross.by])
  }else{
    # ** Changes: use list instead of c and don't think we need return here.
    # and add new col_Nam with refernce to comments below
    return(data[, list(N. = .N,
                       MEAN = lapply(.SD,function(x){round(mean(x,na.rm=T),digits=1)}) , 
                       SD   = lapply(.SD,function(x){round(sd  (x,na.rm=T),digits=2)}) ,
                       'NA' = lapply(.SD,function(x){sum  (is.na(x))}),
                       col_Nam = names(.SD)),
                by=cross.by, .SDcols = traits])
  }
}
trait.by(data.table(iris),traits = c("Sepal.Length", "Sepal.Width"),cross.by="Species")

# result
      Species N. MEAN   SD NA      col_Nam
1:     setosa 50    5 0.35  0 Sepal.Length
2:     setosa 50  3.4 0.38  0  Sepal.Width
3: versicolor 50  5.9 0.52  0 Sepal.Length
4: versicolor 50  2.8 0.31  0  Sepal.Width
5:  virginica 50  6.6 0.64  0 Sepal.Length
6:  virginica 50    3 0.32  0  Sepal.Width