【问题标题】:If element is in a vector, all group takes some value in dplyr如果元素在向量中,则所有组都在 dplyr 中取一些值
【发布时间】:2021-05-20 08:29:51
【问题描述】:

我有一个像 countries

这样的数据集
disjoint_set <- c("CUW", "ARM")

id <- c(1,1,1,1,2,2,2,2,3,3,3,3)
period <- c(1,1,2,2,1,2,3,4,1,1,1,2)
iso <- c("CUW","USA","ARM","SPA","CUW","ARM","CHN","ARM","USA","CHN","ARM","GER")

countries <- data.frame(id, period, iso)

按 id,period 分组如果变量 iso 的某些元素在向量 disjoint_set 中,我想分配 1,否则为 0(我想要定义的组的所有元素为 1 或 0)。新数据集将类似于 countries_not_appear

disjoint = c(1,1,1,1,1,1,0,1,1,1,1,0)
countries_not_appear <- data.frame(id, period, iso, disjoint)

我尝试了以下方法。但这并没有成功

countries_not_appear <- countries %>% group_by(id, period) %>% mutate(disjoint = ifelse(iso %in% disjoint_set, 1, 0))

有什么线索吗?

【问题讨论】:

  • as.numeric(ave(countries$iso, paste(countries$id, countries$period), FUN = function(x) +(any(x %in% disjoint_set))))

标签: r group-by dplyr


【解决方案1】:

ifelse 逐行比较。如果您想检查每个组,请使用any:

library(dplyr)

disjoint_set <- c("CUW", "ARM")

id <- c(1,1,1,1,2,2,2,2,3,3,3,3)
period <- c(1,1,2,2,1,2,3,4,1,1,1,2)
iso <- c("CUW","USA","ARM","SPA","CUW","ARM","CHN","ARM","USA","CHN","ARM","GER")

countries <- data.frame(id, period, iso)

countries_not_appear <- data.frame(id, period, iso, disjoint)
#> Error in data.frame(id, period, iso, disjoint): object 'disjoint' not found

countries %>% 
  group_by(id, period) %>% 
  mutate(disjoint = as.numeric(any(iso %in% disjoint_set)))
#> # A tibble: 12 x 4
#> # Groups:   id, period [8]
#>       id period iso   disjoint
#>    <dbl>  <dbl> <chr>    <dbl>
#>  1     1      1 CUW          1
#>  2     1      1 USA          1
#>  3     1      2 ARM          1
#>  4     1      2 SPA          1
#>  5     2      1 CUW          1
#>  6     2      2 ARM          1
#>  7     2      3 CHN          0
#>  8     2      4 ARM          1
#>  9     3      1 USA          1
#> 10     3      1 CHN          1
#> 11     3      1 ARM          1
#> 12     3      2 GER          0

由reprex package (v2.0.0) 于 2021-05-20 创建

【讨论】:

    【解决方案2】:

    你可以试试下面的代码

    countries %>%
      group_by(id, period) %>%
      mutate(disjoint = +any(iso %in% disjoint_set)) %>%
      ungroup()
    

    给了

    # A tibble: 12 x 4
          id period iso   disjoint
       <dbl>  <dbl> <chr>    <int>
     1     1      1 CUW          1
     2     1      1 USA          1
     3     1      2 ARM          1
     4     1      2 SPA          1
     5     2      1 CUW          1
     6     2      2 ARM          1
     7     2      3 CHN          0
     8     2      4 ARM          1
     9     3      1 USA          1
    10     3      1 CHN          1
    11     3      1 ARM          1
    12     3      2 GER          0
    

    【讨论】:

      【解决方案3】:

      您可以根据您的查询遵循以下代码行 -->

      disjoint_set = c ("CUW", "ARM")
      
      id = c (1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
      
      period = c (1,1,2,2,1, 2, 3, 4, 1,1,1,2)
      
      ISO = c ("CUW", "USA", "ARM", "SPA", "CUW", "ARM", "CHN", "ARM", "USA", "CHN",
               "ARM", "GER")
      
      countries = data.frame (id, period, ISO)
      
      countries
      
      countries $Not.Appear = ifelse (test = countries $ISO == disjoint_set,
                                          yes = 1, no = 0)
      

      Output of the data frame, as required

      【讨论】:

        猜你喜欢
        • 2020-05-14
        • 1970-01-01
        • 2012-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多