列表/多列中的列名称在data.table中分组标准

时间:2017-11-27 01:54:39

标签: r data.table grouping

DT <- data.table(criteria=c("a","b","c","d","d","c","b","a"),
val1=1:8,
val2=c(-1,1,2,3,3,2,1,1),
val3=c(-3,2,0,3,3,0,2,3),
val4=c(5,3,2,1,1,2,3,-5),
val5=c(1,8,5,3,3,5,8,-1))   

    criteria val1 val2 val3 val4 val5
1:        a    1   -1   -3    5    1
2:        b    2    1    2    3    8
3:        c    3    2    0    2    5
4:        d    4    3    3    1    3
5:        d    5    3    3    1    3
6:        c    6    2    0    2    5
7:        b    7    1    2    3    8
8:        a    8    1    3   -5   -1

我想选择按行criteria分组的行(按值sum分组),列val2val5等于零(以函数作为标准的解决方案的奖励积分,因此不限于标准为==0的情况,而不假设我事先知道列名。所以使用colNames <- c('val2','val3','val4','val5')向量。

在上面的例子中,结果应该是第1行和第8行。

如果我不使用其他标准,我可以这样做:

DT[DT[, Reduce(`|`, lapply(.SD, function(x){return(x==0)})), .SDcols = colNames]]

但我无法弄清楚如何添加额外的分组标准。

欢迎任何指示!

1 个答案:

答案 0 :(得分:0)

使用一些基础R和库(magrittr)

<build>
    <plugins>
        <!-- compiler插件, 设定JDK版本 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>xxx.xxx.yourmain</mainClass>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>