匹配具有多个值的列值中的字符串

时间:2015-11-24 12:54:15

标签: r data.table

请帮我解决这个问题 我有一个数据表,如下所示

    dt2 <- data.table(ID = c(1,1,2,2,3,3,4,4,4), variable =     c("a","b","a","c","c","d","e","b","a"))
    dt3 <- dt2[, list(variables = paste(variable, collapse = " | ")), by = ID]
    dt3[,chk:=sample(letters[1:2])]
    dt3
    ID variables chk
1:  1     a | b   b
2:  2     a | c   a
3:  3     c | d   b
4:  4 e | b | a   a

我想过滤变量包含chk的记录。在上面的示例中,应删除第3行。

2 个答案:

答案 0 :(得分:4)

这是一种<sourceDirectory>src</sourceDirectory> <resources> <resource> <directory>src</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> <plugin> <groupId>com.heroku.sdk</groupId> <artifactId>heroku-maven-plugin</artifactId> <version>0.5.6</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <dependencies> <dependency> <groupId>org.apache.maven.shared</groupId> <artifactId>maven-filtering</artifactId> <version>1.3</version> </dependency> </dependencies> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <appName>young-anchorage-8143</appName> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> 方法。它在操作中逐个元素地获取两个参数:

mapply

答案 1 :(得分:1)

我们可以将pastegrep

一起使用
dt3[with(dt3, grep(paste(chk, collapse='|'), variables)),]
#     ID variables chk
#1:  1     a | b   a
#2:  2     a | c   b
#3:  4 e | b | a   b