R:如果在相应行中找不到某个数字,则打印 ID 值
R: print ID value if a certain number is not found in the corresponding rows
我有一个如下所示的数据框:
id
score
x
1
x
2
x
3
y
1
y
2
y
3
...
我想检查每个 ID 在“分数”列中是否有 1、2 和 3。如果一些 ID 没有 1、2 或 3,我想将它们保存到一个向量中。
我试图以某种方式循环它/在 dplyr 中写入条件但失败了:
group_by(id) %>%
{if(!1 %in% score | !2 %in% score | !3 %in% score ) {print(id)}}```
按 'id' 分组后,filter
通过创建逻辑向量并包装 all
即如果所有 1、2、3 都在 'score' 中,然后我们否定 (!
) 以仅过滤那些不符合它的组,得到 distinct
'id' 和 pull
作为 vector
library(dplyr)
v1 <- df1 %>%
group_by(id) %>%
filter(!all(c(1, 2, 3) %in% score)) %>%
ungroup %>%
distinct(id) %>%
pull(id)
-输出
> v1
[1] "z"
注意:print
只是在控制台中打印输出,没有 return 值。我们可能需要存储在一个对象中
在 OP 的代码中,带有 if
的条件参数在 tidyverse 函数之外,并且 score
不是全局环境中的对象,而是数据中的一列。我们可能会用.$
或.[[
来提取,但那样也会失去分组属性。最好在 tidyverse 函数中执行此操作,即 filter
或 summarise
等。或者我们可以使用 group_modify
根据 OP 的代码
执行 print
df1 %>%
group_by(id) %>%
group_modify(~ {if(!1 %in% .x$score | !2 %in% .x$score |
!3 %in% .x$score ) {
print(.y$id)
}
.x})
[1] "z"
# A tibble: 8 x 2
# Groups: id [3]
id score
<chr> <int>
1 x 1
2 x 2
3 x 3
4 y 1
5 y 2
6 y 3
7 z 1
8 z 2
数据
df1 <- structure(list(id = c("x", "x", "x", "y", "y", "y", "z", "z"),
score = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L)),
class = "data.frame", row.names = c(NA,
-8L))
我有一个如下所示的数据框:
id | score |
---|---|
x | 1 |
x | 2 |
x | 3 |
y | 1 |
y | 2 |
y | 3 |
... |
我想检查每个 ID 在“分数”列中是否有 1、2 和 3。如果一些 ID 没有 1、2 或 3,我想将它们保存到一个向量中。
我试图以某种方式循环它/在 dplyr 中写入条件但失败了:
group_by(id) %>%
{if(!1 %in% score | !2 %in% score | !3 %in% score ) {print(id)}}```
按 'id' 分组后,filter
通过创建逻辑向量并包装 all
即如果所有 1、2、3 都在 'score' 中,然后我们否定 (!
) 以仅过滤那些不符合它的组,得到 distinct
'id' 和 pull
作为 vector
library(dplyr)
v1 <- df1 %>%
group_by(id) %>%
filter(!all(c(1, 2, 3) %in% score)) %>%
ungroup %>%
distinct(id) %>%
pull(id)
-输出
> v1
[1] "z"
注意:print
只是在控制台中打印输出,没有 return 值。我们可能需要存储在一个对象中
在 OP 的代码中,带有 if
的条件参数在 tidyverse 函数之外,并且 score
不是全局环境中的对象,而是数据中的一列。我们可能会用.$
或.[[
来提取,但那样也会失去分组属性。最好在 tidyverse 函数中执行此操作,即 filter
或 summarise
等。或者我们可以使用 group_modify
根据 OP 的代码
print
df1 %>%
group_by(id) %>%
group_modify(~ {if(!1 %in% .x$score | !2 %in% .x$score |
!3 %in% .x$score ) {
print(.y$id)
}
.x})
[1] "z"
# A tibble: 8 x 2
# Groups: id [3]
id score
<chr> <int>
1 x 1
2 x 2
3 x 3
4 y 1
5 y 2
6 y 3
7 z 1
8 z 2
数据
df1 <- structure(list(id = c("x", "x", "x", "y", "y", "y", "z", "z"),
score = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L)),
class = "data.frame", row.names = c(NA,
-8L))