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

扶醉桌前 提交于 2019-12-01 10:24:58

问题


I am new to r and I have a dataframe very close to the one below and I would love to find a general way that tells me how many times plus 1, the number "0" appears for each country (intro4) and id.

        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

I can do it by hand by it is a big data frame and would take forever, count () gives me the frequency but doesn't divide it between different countries. I have found a way to do it but I will have to select and filter for each individual county (intro4) and add 1 to the result. I was wondering if there was any quicker way to fo it. The code I have tried was this one:

projects <- finalr %>% select (Intro4,number,id)

projects1<-projects %>%  filter (str_detect (number, "0"))

projects2<-projects1 %>%arrange (Intro4)

projects3<-sum(projects2$Intro4 == "TAN", na.rm = TRUE)

projects4<-sum(projects2$Intro4=="UGA",na.rm=TRUE)

I would be extremely grateful for any help, thank you :)


回答1:


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)



回答2:


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


来源:https://stackoverflow.com/questions/51126352/counting-the-number-of-times-a-value-appears-in-a-column-in-relation-to-other-co

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!