【发布时间】:2021-12-28 16:49:53
【问题描述】:
我正在使用dplyr::filter 函数根据Sex,Patient.Age,Country.where.Event.occurred 三个变量过滤数据,第一个代码段生成正确的结果,第二个代码段生成错误的结果。但是,从我的角度来看,两个代码部分都有相同的表达式,所以我很困惑为什么结果不同。
> data
# A tibble: 1,360 × 3
Sex Patient.Age Country.where.Event.occurred
<chr> <chr> <chr>
1 Female 12 YR US
2 Female 16 YR KW
3 Female 16 YR US
4 Female 16 YR US
5 Female 16 YR US
6 Female 16 YR US
7 Female 17 YR ES
8 Female 17 YR ES
9 Female 17 YR GB
10 Female 19 YR CA
# … with 1,350 more rows
# unique combination of 3 variables
> key <- data %>%
+ distinct(Sex, Patient.Age,Country.where.Event.occurred)
> key
# A tibble: 399 × 3
Sex Patient.Age Country.where.Event.occurred
<chr> <chr> <chr>
1 Female 12 YR US
2 Female 16 YR KW
3 Female 16 YR US
4 Female 17 YR ES
5 Female 17 YR GB
6 Female 19 YR CA
7 Female 19 YR US
8 Female 2 YR US
9 Female 26 YR US
10 Female 28 YR US
# … with 389 more rows
> data %>%
+ filter(Sex == key[3,]$Sex,
+ Patient.Age == key[3,]$Patient.Age,
+ Country.where.Event.occurred == key[3,]$Country.where.Event.occurred)
# A tibble: 4 × 3
Sex Patient.Age Country.where.Event.occurred
<chr> <chr> <chr>
1 Female 16 YR US
2 Female 16 YR US
3 Female 16 YR US
4 Female 16 YR US
> Sex <- key[3,]$Sex
> Sex
[1] "Female"
> Age <- key[3,]$Patient.Age
> Age
[1] "16 YR"
> Country <- key[3,]$Country.where.Event.occurred
> Country
[1] "US"
> data %>%
+ filter(Sex == Sex,
+ Patient.Age == Age,
+ Country.where.Event.occurred == Country)
# A tibble: 7 × 3
Sex Patient.Age Country.where.Event.occurred
<chr> <chr> <chr>
1 Female 16 YR US
2 Female 16 YR US
3 Female 16 YR US
4 Female 16 YR US
5 Male 16 YR US
6 Male 16 YR US
7 Male 16 YR US
【问题讨论】:
标签: r dplyr environment-variables