简体   繁体   English

r-dplyr:计算同一数据帧中另一个变量的每个唯一值的一个变量中唯一值的频率

[英]r - dplyr: counting the frequency of unique values in one variable for each unique value of another variable in the same data frame

So here's a sample of some of the rows from my dataframe: 因此,这是我数据框中的一些行的示例:

> data[1:25, c("TR_DATE", "TR_TYPE...")]
  TR_DATE TR_TYPE...
1  2016-03-01          4
2  2016-03-01          4
3  2016-03-01          5
4  2016-03-01          4
5  2016-03-01          1
6  2016-03-01          7
7  2016-03-01          4
8  2016-03-01          4
9  2016-03-01          24
10 2016-03-01          23
11 2016-03-01          4
12 2016-03-02          4
13 2016-03-02          1
14 2016-03-02          1
15 2016-03-02          4
16 2016-03-02          4
17 2016-03-02          14
18 2016-03-02          4
19 2016-03-02          4
20 2016-03-03          4
21 2016-03-03          1
22 2016-03-03          4
23 2016-03-03          23
24 2016-03-03          1
25 2016-03-03          4

What I'd like to do exactly is rearrange in such a way that for every unique day, I get the number of unique transaction types and the frequency of each transaction type 我想做的正是按照这样的方式重新安排:对于每一天,我都会得到唯一交易类型的数量以及每种交易类型的频率

Here's the code that I tried: 这是我尝试的代码:

data %>%
group_by(TR_DATE) %>%
summarise(trancount = n(), trantype = n_distinct(TR_TYPE...))

which gave me part of the result that I wanted: 这给了我想要的部分结果:

# A tibble: 68 x 3
  TR_DATE trancount trantype
   <date>     <int>    <int>
 1 2016-03-01      5816        6
 2 2016-03-02      5637        3
 3 2016-03-03      4818        3
 4 2016-03-04      5070        8
 5 2016-03-05         4        2
 6 2016-03-08      6707        5
 7 2016-03-09      5228        5
 8 2016-03-10      4722        6
 9 2016-03-11      4469        8
10 2016-03-12         1        1
# ... with 58 more rows

so trantype tells me the number of unique transaction types that happened on a particular day, but I'd like to know the frequency of each of these unique transaction types. 因此trantype告诉我在特定日期发生的唯一交易类型的数量,但是我想知道每种独特交易类型的频率。 What would be the best way to go around doing this? 最好的方法是做什么? I tried looking around and found similar questions but was unable to modify the solutions to my requirement. 我尝试环顾四周,发现类似的问题,但无法修改解决方案以满足我的要求。 I'm fairly new to R and would really appreciate some help. 我对R相当陌生,非常感谢您的帮助。 Thanks. 谢谢。

You should group by both variables: 您应该按两个变量进行分组:

data %>%
group_by(TR_DATE, TR_TYPE...) %>%
summarise(trancount = n(), trantype = n_distinct(TR_TYPE...))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个变量的唯一值在另一个变量中分组的频率 - R? - Frequency of unique values of one variable grouped in another variable - R? 在R中使用聚合来查找另一个变量的相同值的一个变量的唯一值 - Using aggregate in R to find unique values of one variable for the same value of another variable SQLDF R:计算数据框中的唯一值 - SQLDF R: Counting unique values in a data frame 使用data.table优化计数一个变量的唯一值的数量 - Optimizing counting the number of unique values of one variable by another with data.table R dplyr 将一个数据帧中的唯一值替换为具有不同行号的其他数据帧中的唯一值 - R dplyr Replace unique values from one data frame with unique values from other data frame with unequal row numbers R:通过数据框中另一个变量的唯一值计算变量的平均值? - R: Calculate the mean value of a variable by unique values of another variable in a dataframe? 如何在R Data框架的新列中获取变量的唯一值? - How to get the unique values of a variable in new columns in a R Data frame? 将唯一值从一个数据框中复制到 R 中的另一个 - Copy Unique values from one data frame to another in R 如何根据另一个数据框中唯一值的数量创建变量? - How to create a variable based on the number of unique values in another data frame? 在 R dplyr 中按其他变量的唯一值对多个变量进行分组 - group by in R dplyr for more than one variable on unique value of other variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM