counting the number of times a value appears in a column in relation to other columns in r

偶尔善良 提交于 2019-12-01 13:45:49

Assuming number can be anything like 0, 1, 2 etc. one can count occurrence of 0 by sum(number==0). A solution using dplyr can be as:

library(dplyr)

df %>% group_by(Intro4, id) %>%
  summarise(count = sum(number==0))

# # A tibble: 9 x 3
# # Groups: Intro4 [?]
#   Intro4    id count
#   <chr>  <int> <int>
# 1 TAN       19     1
# 2 TAN       73     2
# 3 TOG       37     1
# 4 TOG       58     1
# 5 UGA       96     2
# 6 UGA      112     1
# 7 ZAM       40     1
# 8 ZAM       99     1
# 9 ZAM      139     1

Data:

df <- read.table(text="
Intro4    number  id
221    TAN           0  19
222    TAN           0  73
223    TAN           0  73
224    TOG           0  37
225    TOG           0  58
226    UGA           0  96
227    UGA           0 112
228    UGA           0  96
229    ZAM           0  40
230    ZAM           0  99
231    ZAM           0 139",
header = TRUE, stringsAsFactors = FALSE)

You can also do it as followed:

library(dplyr)
dat <- read.table(header = T, text = 
                    "Intro4    number  id

                      TAN           0  19
                      TAN           0  73
                      TAN           0  73
                      TOG           0  37
                      TOG           0  58
                      UGA           0  96
                      UGA           0 112
                      UGA           0  96
                      ZAM           0  40
                      ZAM           0  99
                      ZAM           0 139", stringsAsFactors = F)
dat %>% group_by(Intro4, id, number) %>% tally()

Which produces:

  Intro4    id number     n
  <chr>  <int>  <int> <int>
1 TAN       19      0     1
2 TAN       73      0     2
3 TOG       37      0     1
4 TOG       58      0     1
5 UGA       96      0     2
6 UGA      112      0     1
7 ZAM       40      0     1
8 ZAM       99      0     1
9 ZAM      139      0     1
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!