【问题标题】:new column variable "count" based on existing columns in transaction dataset基于交易数据集中现有列的新列变量“count”
【发布时间】:2019-12-20 18:27:25
【问题描述】:

我有一个包含三列的交易数据集。每行代表一个事务。

  Account_from  Account_to  Value 
1       1           2        25.0
2       1           3        30.0
3       2           1        28.0
4       2           3        10.0
5       2           4        12.0
6       3           1        40.0

我想创建新的列变量,其中包含有关每个帐户进行和接收的交易数量的信息(两列)。它看起来像下面这样:

  Account_from  Account_to  Value  Count_out  Count_in 
1       1           2        25.0      2          2
2       1           3        30.0      2          2
3       2           1        28.0      3          1
4       2           3        10.0      3          1
5       2           4        12.0      3          1
6       3           1        40.0      1          2

如何一次对整个数据集执行此操作?

【问题讨论】:

  • 您能说明失败的原因吗?

标签: r


【解决方案1】:

tidyverse 提供了有用的功能 - 假设您的数据存储在数据框 df 中:

library(tidyverse)
df <- df %>% add_count(Account_from, name = "Count_out") %>%
             add_count(Account_to, name = "Count_in")

【讨论】:

    【解决方案2】:

    这是基于 R 的解决方案,其中使用了ave()

    df <- within(df, 
                 list(Count_out <- ave(1:nrow(df),Account_from,FUN = length),
                      Count_in <- ave(1:nrow(df),Account_to,FUN = length)[match(Account_from,Account_to,)]))
    

    这样

    > df
      Account_from Account_to Value Count_in Count_out
    1            1          2    25        2         2
    2            1          3    30        2         2
    3            2          1    28        1         3
    4            2          3    10        1         3
    5            2          4    12        1         3
    6            3          1    40        2         1
    

    或者使用下面的代码:

    df <- cbind(df, with(df, list(Count_out = ave(1:nrow(df),Account_from,FUN = length), 
                                  Count_in = ave(1:nrow(df),Account_to,FUN = length)[match(Account_from,Account_to,)])))
    

    这样

    > df
      Account_from Account_to Value Count_out Count_in
    1            1          2    25         2        2
    2            1          3    30         2        2
    3            2          1    28         3        1
    4            2          3    10         3        1
    5            2          4    12         3        1
    6            3          1    40         1        2
    

    数据

    df <- structure(list(Account_from = c(1L, 1L, 2L, 2L, 2L, 3L), Account_to = c(2L, 
    3L, 1L, 3L, 4L, 1L), Value = c(25, 30, 28, 10, 12, 40), Count_out = c(2L, 
    2L, 3L, 3L, 3L, 1L), Count_in = c(2L, 2L, 1L, 1L, 1L, 2L)), class = "data.frame", row.names = c(NA, 
    -6L))
    

    【讨论】:

    • @florisviss 这是一个基于 R 的解决方案,因此代码看起来可能比带有包 dplyrtidyverse 的代码更长
    【解决方案3】:

    我们可以通过使用dplyr 的一些连接操作来做到这一点。

    library(dplyr)
    
    inner_join(df %>% count(Account_from, name = 'Count_out'), 
               df %>% count(Account_to, name = 'Count_in'), 
               by = c('Account_from' = 'Account_to')) %>%
    right_join(df) %>%
    select(names(df), Count_out, Count_in)
    
    #  Account_from Account_to Value Count_out Count_in
    #         <int>      <int> <dbl>     <int>    <int>
    #1            1          2    25         2        2
    #2            1          3    30         2        2
    #3            2          1    28         3        1
    #4            2          3    10         3        1
    #5            2          4    12         3        1
    #6            3          1    40         1        2
    

    【讨论】:

      猜你喜欢
      • 2015-08-16
      • 2018-12-25
      • 1970-01-01
      • 2020-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多