【问题标题】:Conditional Sequency Count, Paste Specific Columns条件序列计数,粘贴特定列
【发布时间】:2016-06-17 15:33:06
【问题描述】:

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

id | seq_check | action | ct
123 | end | action_a | 1  
123 | start | action_b | 4  
123 | start | action_c | 1  
456 | end | action_d | 1  
456 | start | action_e | 16  
456 | start | action_f | 4  
456 | start | action_g | 5  
456 | start | action_h | 2  
456 | start | action_i | 1

'end' 标签每个 id 只出现一次,是一个 id 的特定序列的端点。我想要的是一个如下所示的数据框:

id | seq_action | ct  
123 | action_a <- action_b | 4  
123 | action_a <- action_c | 1  
456 | action_d <- action_e | 16  
456 | action_d <- action_f | 4  
456 | action_d <- action_g | 5  
456 | action_d <- action_h | 2  
456 | action_d <- action_i | 1  

有人知道我如何在 R 中做到这一点吗?非常感谢!

【问题讨论】:

    标签: r dataframe conditional-formatting


    【解决方案1】:

    您也可以使用dplyrtidyr

    library(dplyr); library(tidyr);
    
    spread(df, seq_check, action) %>% fill(end) %>% 
          mutate(seq_action = paste(end, start, sep = " <- ")) %>% 
          select(id, seq_action, ct)
    
       id           seq_action ct
    1 123 action_a <- action_c  1
    2 123 action_a <- action_b  4
    3 456 action_d <- action_i  1
    4 456 action_d <- action_h  2
    5 456 action_d <- action_f  4
    6 456 action_d <- action_g  5
    7 456 action_d <- action_e 16
    

    【讨论】:

      【解决方案2】:

      我们可以使用data.table。将'data.frame'转换为'data.table'(setDT(df1)),按'id'分组,我们paste'seq_check'中'end'对应的'action'与'action'对应' 'seq_check' 中的 start',以及子集 'ct',其中 'seq_check' 是 'start'。

      library(data.table)
      setDT(df1)[,.(seq_action=paste(action[seq_check=="end"],action[seq_check=="start"],
                    sep=" <- "), ct = ct[seq_check=="start"]) , by =  id]
      #    id           seq_action ct
      #1: 123 action_a <- action_b  4
      #2: 123 action_a <- action_c  1
      #3: 456 action_d <- action_e 16
      #4: 456 action_d <- action_f  4
      #5: 456 action_d <- action_g  5
      #6: 456 action_d <- action_h  2
      #7: 456 action_d <- action_i  1
      

      注意:只使用一个包。


      或者使用na.locfdcast

      library(zoo)
      dcast(setDT(df1), id+ct~seq_check, value.var = "action")[, .(id, 
                    seq_action=paste(na.locf(end), start, sep=" <- "), ct)]
      

      【讨论】:

        猜你喜欢
        • 2021-08-22
        • 2018-02-05
        • 1970-01-01
        • 1970-01-01
        • 2016-04-20
        • 2020-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多