2017-03-21 34 views
2

我如何匹配多个模式并获取与该模式对应的值。如何匹配字符串中的多个模式?

我有这样的一个表:

library(data.table) 
set.seed(1) 
table_1 <- data.table(names = c('bluecdsd','red321','yellowVsds523','423_black','ewrwblack'), 
         value = runif(5)) 

而且这样的模式表:

table_2 <- data.table(category = c('black','blue','red','white'), 
         size = c('small','little','large','huge')) 

我想要的结果:

  names  value size 
1:  bluecdsd 0.5995658 little 
2:  red321 0.4935413 large 
3: yellowVsds523 0.1862176  NA 
4:  423_black 0.8273733 small 
5:  ewrwblack 0.6684667 small 

我知道我应该使用正则表达式,但不知道如何匹配多个模式,任何帮助请问?

回答

1

grep category of table2 with names of table1 and get the values of names of table1 and assign it to table2。一旦我们在两个表中都有names,我们可以使用基于on = .(names)join方法,并将表2中的size绑定到table1。

library(data.table)  
    table_2 <- table_2[, .(names = grep(unique(category), table_1[, names], value = TRUE ), size = size), 
        by = category ] 
    table_2 <- table_2[!is.na(names), ] 

    table_1[table_2, `:=` (size = i.size), on = c('names')] 
    table_1 
    #   names  value size 
    # 1:  bluecdsd 0.2655087 little 
    # 2:  red321 0.3721239 large 
    # 3: yellowVsds523 0.5728534  NA 
    # 4:  423_black 0.9082078 small 
    # 5:  ewrwblack 0.2016819 small 

数据:

set.seed(1) 
table_1 <- data.table(names = c('bluecdsd','red321','yellowVsds523','423_black','ewrwblack'), 
         value = runif(5)) 

table_2 <- data.table(category = c('black','blue','red','white'), 
         size = c('small','little','large','huge')) 
1

我们可以extract子串并做match

library(stringr) 
table_1[, size := table_2$size[match(str_extract(names, 
      paste(table_2$category, collapse="|")), table_2$category)]] 
table_1 
#   names  value size 
#1:  bluecdsd 0.2655087 little 
#2:  red321 0.3721239 large 
#3: yellowVsds523 0.5728534  NA 
#4:  423_black 0.9082078 small 
#5:  ewrwblack 0.2016819 small