在data.table中使用do.call和.SD中的列来应用函数

时间:2018-05-07 21:29:46

标签: r data.table lapply do.call

我尝试创建stop_gradient / tf.gradients的变体,该变体使用tf.assign参数跨越任意一组列,使用{{1 } / pmax。下面的函数的第一个版本对过滤器值进行硬编码,但适用于pmin

filter_value

返回:

.SD

下面的函数的第二个版本需要第二个.SDcols参数,但是我无法使用.SD,而是必须将各个列向量作为列表让事情发挥作用:

testFuncV1 <- function(...) { 
  cols <- list(...)
  num_cols <- length(cols)
  num_records <- length(cols[[1]])
  max_records <- c()
  for (record_num in 1:num_records) {
    v <- c()
    for (l in cols) {
      v <- c(v, l[[record_num]])
    }
    filt_v <- Filter(function(x) { x <= 1 }, v)
    if (length(filt_v) == 0) {
      max_records <- c(max_records, NA) 
    } else {
      max_records <- c(max_records, max(filt_v))
    }
  }
  max_records
}

test_dt_v1 <- data.table(a = c(1,3,5), b = c(2,3,-1), c = c(-3, 5, 2))

test_dt_v1[, max_with_filter := do.call(testFuncV1, .SD), .SDcols = c('a', 'b', 'c')]

也会返回:

   a  b  c max_with_filter
1: 1  2 -3               1
2: 3  3  5              NA
3: 5 -1  2              -1

理想情况下,我可以使用filter找出适合.SD的方法,或者使用testFuncV2 <- function(cols, filter) { num_cols <- length(cols) num_records <- length(cols[[1]]) max_records <- c() for (record_num in 1:num_records) { v <- c() for (l in cols) { v <- c(v, l[[record_num]]) } filt_v <- Filter(function(x) { x <= filter }, v) if (length(filt_v) == 0) { max_records <- c(max_records, NA) } else { max_records <- c(max_records, max(filt_v)) } } max_records } test_dt_v2 <- data.table(a = c(1,3,5), b = c(2,3,-1), c = c(-3, 5, 2)) test_dt_v2[, max_with_filter := do.call(testFuncV2, list(list(test_dt_v2$a, test_dt_v2$b, test_dt_v2$c), 1))] 的替代方法(我也尝试过)周围,​​无济于事。提前谢谢!

1 个答案:

答案 0 :(得分:1)

以下是使用apply(MARGIN=1, ...)

的选项
func <- function(x, threshold) {
    if (any(x <= threshold)) return(max(x[x <= threshold])) 
    NA
}
test_dt_v1[, max_with_filter := apply(.SD, 1, func, threshold=1),
    .SDcols=c("a","b","c")]

使用do.callpmax的另一个选项,首先将值高于1转换为NA(想法来自rowwise maximum for R

test_dt_v1[, max_with_filter := do.call(pmax, c(`is.na<-`(.SD, .SD>1), na.rm=T))]