分组然后变异

问题描述 投票:0回答:1

我有一个数据集,我每年只想为每个人选择一行 - 但是,我想改变一列,这样如果它对任何一个人行说“是”,那么所有行都说“是” .

这是我拥有的数据集的示例:

因此,在名称、诊所和年份相同的情况下,如果该分组的任何其他行都说“是”,我希望测试列说“是”。

因此,这就是我希望数据集最终看起来像的样子:

rstudio mutate
1个回答
0
投票

使用

dplyr
非常简单。这是一个选项:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- tribble(
  ~ name, ~ clinic, ~ year, ~ date, ~ tested,
  "a",       "xxy",    2022,  "April", "yes",
  "a",       "xxy",    2022,  "May", "no",
  "b",       "ggf",    2019,  "Jan", "no",
  "b",       "ggf",    2019,  "Feb", "yes",
  "c",       "ffr",    2018,  "March", "yes",
  "c",       "ffr",    2019,  "May", "no"
)

df |> 
  mutate(tested2 = if_else(any(tested == "yes"), "yes", "no"), .by = c(name, year))
#> # A tibble: 6 × 6
#>   name  clinic  year date  tested tested2
#>   <chr> <chr>  <dbl> <chr> <chr>  <chr>  
#> 1 a     xxy     2022 April yes    yes    
#> 2 a     xxy     2022 May   no     yes    
#> 3 b     ggf     2019 Jan   no     yes    
#> 4 b     ggf     2019 Feb   yes    yes    
#> 5 c     ffr     2018 March yes    yes    
#> 6 c     ffr     2019 May   no     no

创建于 2024-02-25,使用 reprex v2.1.0

我建议在发布未来的问题之前阅读这个问题。这样可以更轻松地帮助您。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.