【问题标题】:Re-organize database by merging rows from a variable通过合并变量中的行来重新组织数据库
【发布时间】:2016-07-15 15:46:22
【问题描述】:

我有一个如下所示的数据库:

userId          SessionId        Screen         Platform       Version
01              1                first          IOS            1.0.1
01              1                main           IOS            1.0.1
01              2                first          IOS            1.0.1
01              3                first          IOS            1.0.1
01              3                main           IOS            1.0.1
01              3                detail         IOS            1.0.1
02              1                first          Android        1.0.2

基本上我打算做的是确定“路径”(不同的屏幕)是否会导致更好的保留。我想将每个 sessionId 重新组织在一列中。理想的数据库应该是这样的:

userId       SessionId       Path                 Retention
01           1               first;main           3
01           2               first                3
01           3               first;main;detail    3
02           1               first                1

这里是变量Retention 将等于最大SessionId

【问题讨论】:

    标签: r database row reorganize


    【解决方案1】:

    基础 R 中的可能解决方案:

    d2 <- aggregate(Screen ~ userId + SessionId, d, toString)
    transform(d2, retention = ave(Screen, userId, FUN = length))
    

    给出:

    > d2
      userId SessionId              Screen retention
    1     01         1         first, main         3
    2     02         1               first         1
    3     01         2               first         3
    4     01         3 first, main, detail         3
    

    使用dplyr的替代方法:

    library(dplyr)
    d %>% 
      group_by(userId, SessionId) %>% 
      summarise(Screen = toString(Screen)) %>% 
      group_by(userId) %>% 
      mutate(retention = n())
    

    给出:

      userId SessionId              Screen retention
       <chr>     <int>               <chr>     <int>
    1     01         1         first, main         3
    2     01         2               first         3
    3     01         3 first, main, detail         3
    4     02         1               first         1
    

    【讨论】:

      【解决方案2】:

      我有一个data.table 解决方案

      library(data.table)
      dt <- as.data.table(d)
      dt[, Retention := max(SessionId), by = .(userId)]
      dt[, .(Screen = paste(Screen, collapse = ";"), Retention = unique(Retention)), by = .(userId, SessionId)]
      
      userId SessionId            Screen Retention
      1:     01         1        first;main         3
      2:     01         2             first         3
      3:     01         3 first;main;detail         3
      4:     02         1             first         1
      

      【讨论】:

      • 一个更干净的选择(至少 imo):dt[, .(Screen = toString(Screen)), by = .(userId, SessionId)][, retention := .N, by = userId][]
      • 我认为分号很重要,并且可能缺少 sessionId。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多