![](/img/trans.png)
[英]R: Create a new column with multiple categories(levels) based on the critera of two other columns
[英]sum the number of occurences based on two columns with 3 levels of categories in R
我有兩列包含分類數據,它們是不同的類別,都具有級別 0,1 和 2。
我想總結組合發生的次數,但總計數只取該列的總和。
Groups: dfMBG$datasetG.snelheid [3]
dfMBG$datasetG.snelheid as.character(dfMBG$~ countMB
<chr> <chr> <dbl>
1 0 0 153
2 0 1 153
3 0 2 153
4 1 0 153
5 1 1 153
6 1 2 153
7 2 0 153
8 2 1 153
9 2 2 153
我希望它看起來像這樣。
Groups: dfMBG$datasetG.snelheid [3]
dfMBG$datasetG.snelheid as.character(dfMBG$~ countMB
<chr> <chr> <dbl>
1 0 0 12
2 0 1 15
3 0 2 45
4 1 0 12
5 1 1 15
6 1 2 28
7 2 0 4
8 2 1 17
9 2 2 5
我使用的代碼是這樣的:
MBGcount<-dfMBG %>% rowwise(.) %>%
group_by(dfMBG$datasetG.snelheid, as.character(dfMBG$datasetG.indicatie)) %>%
summarise(countMB = sum(as.numeric(dfMBG$datasetG.verstoringsbron)))
MBGcount
dfMBG$dfMBG$datasetG.verstoringsbron
由帶有 1 的列組成。
感謝你們對我的幫助!
如果你改為運行它,它是否達到了你想要的效果?
dfMBG %>%
group_by(datasetG.snelheid, datasetG.indicatie) %>%
count()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.