计算字符串中元素的出现

时间:2019-06-16 07:56:57

标签: r dplyr strsplit

我有以下数据集:

 def adminRole = new ShiroRole(name: "Administrator")
 adminRole.addToPermissions("*:*")
 adminRole.save(flush:true, failOnError:true);

“链”列代表产品的购买过程,并且缺少一些信息(开始购买)。目标是对链中的每个值进行两次计数(来源,例如来自目的地,例如)。能够做到这一点,我需要重组数据集。 例如,重组后的链structure(list(ID = c(5L, 6L, 7L, 8L, 10L), chain = c("x49", "x43", "x32 > x42 > x49 > x45 > x20 > x50 > x38", "x54 > x44", "x38 > x38")), row.names = c(NA, -5L), class = c("data.table", "data.frame")) ID chain 1: 5 x49 2: 6 x43 3: 7 x32 > x42 > x49 > x45 > x20 > x50 > x38 4: 8 x54 > x44 5: 10 x38 > x38 应该看起来像这样:

x54 > x44

整个结果应如下所示:

   from  to
1 start x54
2   x54 x44
3   x44 buy

我已经尝试过了,但是我不确定这是否是一个好主意(也不知道如何进行此操作)。

    from  to
1  start x49
2    x49 buy
3  start x43
4    x43 buy
5  start x32
6    x32 x42
7    x42 x49
8    x49 x45
9    x45 x20
10   x20 x50
11   x38 buy
12 start x54
13   x54 x44
14   x44 buy
15 start x54
16   x54 x44
17   x44 buy
18 start x38
19   x38 x38
20   x38 buy

性能可能很重要,因为链会变得很长(30个项目),并且整个数据集都有10万行。

2 个答案:

答案 0 :(得分:1)

R的基本方法是在output$choice <- renderPrint( c(input$selected, input$select) ) 上拆分字符串,并创建一个结合所有值的数据框。

task myCopyTask(type: Copy) {
    eachFile {
        path -= ~/^.+?\//
    }
    from tarTree(resources.gzip('mytarfile.tar.gz'))
    into destinationDir

    includeEmptyDirs = false // ignore empty directories
}

使用类似的方法" > "将是

do.call(rbind, lapply(strsplit(df$chain, " > "), function(x) 
               data.frame(from = c("start",x), to = c(x, "buy"))))

#    from  to
#1  start x49
#2    x49 buy
#3  start x43
#4    x43 buy
#5  start x32
#6    x32 x42
#7    x42 x49
#8    x49 x45
#9    x45 x20
#10   x20 x50
#11   x50 x38
#12   x38 buy
#13 start x54
#14   x54 x44
#15   x44 buy
#16 start x38
#17   x38 x38
#18   x38 buy

答案 1 :(得分:1)

我们可以将字符串粘贴在str_c的开头和结尾,使用separate_rowstidyverse扩展数据集

library(tidyverse)
dt %>%
   mutate(chain = str_c("start > ", chain, " > buy")) %>%
   separate_rows(chain) %>% group_by(ID) %>% 
   transmute(from = chain, to = lead(chain)) %>% 
   na.omit %>% 
   ungroup %>% 
   select(-ID)
# A tibble: 18 x 2
#   from  to   
#   <chr> <chr>
# 1 start x49  
# 2 x49   buy  
# 3 start x43  
# 4 x43   buy  
# 5 start x32  
# 6 x32   x42  
# 7 x42   x49  
# 8 x49   x45  
# 9 x45   x20  
#10 x20   x50  
#11 x50   x38  
#12 x38   buy  
#13 start x54  
#14 x54   x44  
#15 x44   buy  
#16 start x38  
#17 x38   x38  
#18 x38   buy