R-分组和计数?
我有一个数据集,它有两列R-分组和计数?,r,R,我有一个数据集,它有两列matchid和roundnumber,看起来像: matchid roundnumber 1522380841 1 1522380841 2 1522380841 1 1522380841 3 1522380841 2 1522380841 1 1522380841 1 1522380842 2 1522380842 2 1522380842 3 1522380842 1 1522380842 4 152238084
matchid
和roundnumber
,看起来像:
matchid roundnumber
1522380841 1
1522380841 2
1522380841 1
1522380841 3
1522380841 2
1522380841 1
1522380841 1
1522380842 2
1522380842 2
1522380842 3
1522380842 1
1522380842 4
1522380842 1
我无法计算单个matchid
和roundnumber
应该存在的总次数。
对于本例,输出应为:
count (matchid)
2
我认为它可能需要一个唯一的约束?对于每个matchid
,可以有重复的roundnumber
值,但我只需要对它们进行一次计数。我只需要找出存在多少唯一的matchid
我尝试使用dplyr:
library(dplyr)
count(r6,var=r6$matchid,r6$roundnumber)
但我认为它不能正常工作。我认为table函数正是您需要的:
table(r6$matchid)
例如:
letters = c('a', 'a', 'a', 'b', 'b', 'a', 'c')
table(letters)
然后将其更改为数据帧可能会很方便:
data.frame(table(letters))
如果您坚持使用dplyr解决方案
letters = c('a', 'a', 'a', 'b', 'b', 'a', 'c')
library(dplyr)
df <- data.frame(letters)
df %>% group_by(letters) %>% summarise(n())
# A tibble: 3 x 2
letters `n()`
<fct> <int>
1 a 4
2 b 2
3 c 1
字母=c('a','a','a','b','b','a','c')
图书馆(dplyr)
df%分组依据(字母)%>%总结(n())
#一个tibble:3x2
字母'n()`
1 a 4
2 b 2
3 c 1
使用data.table包非常简单:
library(data.table)
# asuming your dataset is named "df"
df <- data.table(df)
df <- df[, list(count=.N), by=matchid]
我在这个问题上犯了一个错误。这是不完整的。我现在无法删除该问题。@SakshamChawla如果问题不完整,请通过编辑添加更多信息。@avid_用户编辑。我忘记了一个重要的列。您是否正在尝试查找每个matchid
+roundnumber
组合的计数?您的输出中不应该有roundnumber
吗?您需要这样的内容:表(粘贴(r6$matchid,r6$roundnumber,sep=''.')?我只是想知道打了多少场比赛。每个唯一的matchid
代表一场比赛,但是roundnumber
可以为每个matchid
重复。我不确定如何做,或者我问的问题是否正确。长度(唯一(r6$matchid))
?如果您只计算唯一编号的编号,则不确定为什么需要roundnumber
matches@avid_useR我是新手,我仍然不知道回答我问题的语法。出于好奇,如果我想知道每场比赛有多少独特的回合,我会怎么做?
head(df)
matchid count
1522380841 7
1522380842 6
.
.