基于交易数据集中现有列的新列变量“ count”

时间:2019-12-20 09:53:03

标签: r

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

  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

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

我们可以使用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

答案 2 :(得分:0)

这是在基数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))