计算一个值相对于r中其他列的出现次数 [英] counting the number of times a value appears in a column in relation to other columns in r

查看:83
本文介绍了计算一个值相对于r中其他列的出现次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是r的新手,我有一个非常接近于以下数据框的数据框,我很想找到一种通用的方式来告诉我多少次加1,每个国家(前导4)出现的数字为 0, ID。

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

我可以手工完成因为它是一个大数据框架,并且会花很长时间,所以count()给了我频率,但是没有在不同国家之间划分频率。我找到了一种方法,但是我必须为每个县选择和过滤(intro4),然后在结果中加1。我想知道是否有更快的方法可以做到这一点。我尝试过的代码是这样的:

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 :)

推荐答案

假设数字可能类似于 0 1 2 等。一个可以计算 0 sum(number == 0)。使用 dplyr 的解决方案可以是:

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

数据:

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)

这篇关于计算一个值相对于r中其他列的出现次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆