简体   繁体   English

data.table 子集条件 on.SD 列?

[英]data.table subset condition on .SD columns?

I want to create the same result as the below minimum reproducible example, but you can see the subsetting condition on the last code is basically remove rows where.SD columns should not be all zero .我想创建与以下最小可重现示例相同的结果,但您可以看到最后一个代码的子集条件基本上是删除 rows where.SD columns should not be all zero

library(dplyr)

library(data.table)

set.seed(1)

t1 <- sample(-1:1, 999999, replace = T) %>% matrix(ncol = 9) %>% as.data.table %>% {cbind.data.frame(id = 1:nrow(.), .)}

t1[V1 != 0|V2 != 0|V3 != 0|V4 != 0|V5 != 0|V6 != 0|V7 != 0|V8 != 0|V9 != 0, lapply(.SD, sum), by = id]

I have tried我努力了

t1[sum(abs(.SD)) != 0, lapply(.SD, sum), by = id]

We can create the i with Reduce我们可以用Reduce创建i

i1 <- t1[, .I[Reduce(`|`, lapply(.SD, `!=`, 0))], .SDcols = V1: V9]
out2 <- t1[i1, lapply(.SD, sum), by = id]

-checking with OP's output -检查 OP 的 output

out1 <- t1[V1 != 0|V2 != 0|V3 != 0|V4 != 0|V5 != 0|V6 != 0|V7 != 0|V8 != 0|V9 != 0, lapply(.SD, sum), by = id]  
identical(out1, out2)
#[1] TRUE

Using rowSums() :使用rowSums()

t1[t1[, rowSums(.SD != 0) > 0, .SD = patterns("V\\d")]
   ][, lapply(.SD, sum), by = id] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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