在 r 中提取行
extracting rows in r
我做了一个最小可复现的例子,但是我的真实数据真的很大
sat_score<-c(100,4,30,4,20,77,99)
state <-c("NC","NC","CA","WA","NC","SC","NY")
id <- 1: 7
score_1 <-c(1, 1, 0.99, 1, 1, 1, 1)
score_2 <-c(1, 0.99, 1, 1, 1, 1, 1)
score_3 <-c(1, 0.99, 1, 1, 1, 1, 0.99)
score_4 <-c(1, 1, 0.99, 1, 1, 0.99, 1)
data<-data.frame(sat_score,state,id,score_1,score_2,score_3,score_4)
所以,数据是这样的:
sat_score state id score_1 score_2 score_3 score_4
1 100 NC 1 1.00 1.00 1.00 1.00
2 4 NC 2 1.00 0.99 0.99 1.00
3 30 CA 3 0.99 1.00 1.00 0.99
4 4 WA 4 1.00 1.00 1.00 1.00
5 20 NC 5 1.00 1.00 1.00 1.00
6 77 SC 6 1.00 1.00 1.00 0.99
7 99 NY 7 1.00 1.00 0.99 1.00
所有分数(在这个例子中,我有 3 个分数,但在我的真实数据中,它有 15 个分数)
我想提取至少有一个分数不为 1 的行(人)。
例如,在此示例中,应提取 ID 2、3、6、7 的行,因为它们的分数之一 is/are 不是 1(但应保留所有列)
我该怎么做?
使用 tidyverse:
library(tidyverse)
sat_score <- c(100,4,30,4,20,77,99)
state <- c("NC","NC","CA","WA","NC","SC","NY")
id <- 1:7
score_1 <- c(1, 1, 0.99, 1, 1, 1, 1)
score_2 <- c(1, 0.99, 1, 1, 1, 1, 1)
score_3 <- c(1, 0.99, 1, 1, 1, 1, 0.99)
score_4 <- c(1, 1, 0.99, 1, 1, 0.99, 1)
data <- data.frame(sat_score, state,id, score_1, score_2, score_3, score_4)
data %>%
filter(if_any(starts_with("score"), ~ . < 1))
sat_score state id score_1 score_2 score_3 score_4
1 4 NC 2 1.00 0.99 0.99 1.00
2 30 CA 3 0.99 1.00 1.00 0.99
3 77 SC 6 1.00 1.00 1.00 0.99
4 99 NY 7 1.00 1.00 0.99 1.00
我做了一个最小可复现的例子,但是我的真实数据真的很大
sat_score<-c(100,4,30,4,20,77,99)
state <-c("NC","NC","CA","WA","NC","SC","NY")
id <- 1: 7
score_1 <-c(1, 1, 0.99, 1, 1, 1, 1)
score_2 <-c(1, 0.99, 1, 1, 1, 1, 1)
score_3 <-c(1, 0.99, 1, 1, 1, 1, 0.99)
score_4 <-c(1, 1, 0.99, 1, 1, 0.99, 1)
data<-data.frame(sat_score,state,id,score_1,score_2,score_3,score_4)
所以,数据是这样的:
sat_score state id score_1 score_2 score_3 score_4
1 100 NC 1 1.00 1.00 1.00 1.00
2 4 NC 2 1.00 0.99 0.99 1.00
3 30 CA 3 0.99 1.00 1.00 0.99
4 4 WA 4 1.00 1.00 1.00 1.00
5 20 NC 5 1.00 1.00 1.00 1.00
6 77 SC 6 1.00 1.00 1.00 0.99
7 99 NY 7 1.00 1.00 0.99 1.00
所有分数(在这个例子中,我有 3 个分数,但在我的真实数据中,它有 15 个分数) 我想提取至少有一个分数不为 1 的行(人)。
例如,在此示例中,应提取 ID 2、3、6、7 的行,因为它们的分数之一 is/are 不是 1(但应保留所有列)
我该怎么做?
使用 tidyverse:
library(tidyverse)
sat_score <- c(100,4,30,4,20,77,99)
state <- c("NC","NC","CA","WA","NC","SC","NY")
id <- 1:7
score_1 <- c(1, 1, 0.99, 1, 1, 1, 1)
score_2 <- c(1, 0.99, 1, 1, 1, 1, 1)
score_3 <- c(1, 0.99, 1, 1, 1, 1, 0.99)
score_4 <- c(1, 1, 0.99, 1, 1, 0.99, 1)
data <- data.frame(sat_score, state,id, score_1, score_2, score_3, score_4)
data %>%
filter(if_any(starts_with("score"), ~ . < 1))
sat_score state id score_1 score_2 score_3 score_4
1 4 NC 2 1.00 0.99 0.99 1.00
2 30 CA 3 0.99 1.00 1.00 0.99
3 77 SC 6 1.00 1.00 1.00 0.99
4 99 NY 7 1.00 1.00 0.99 1.00