【问题标题】:counting the number of times a value appears in a column in relation to other columns in r计算一个值相对于 r 中其他列出现在列中的次数
【发布时间】:2018-07-01 19:27:15
【问题描述】:

我是 r 的新手,我有一个非常接近下面的数据框,我很想找到一种通用方法,告诉我多少次加 1,每个国家/地区出现数字“0”(intro4)和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。我想知道是否有更快的方法来解决它。我试过的代码是这样的:

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)

如果有任何帮助,我将非常感激,谢谢:)

【问题讨论】:

  • table(projects2[projects2$number==0,c(1,3)]) 怎么样?

标签: r dplyr tidyverse


【解决方案1】:

你也可以这样做:

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

产生:

  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

【讨论】:

    【解决方案2】:

    假设number 可以是012 等任何东西。可以通过sum(number==0) 计算0 的出现次数。使用dplyr 的解决方案可以是:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多