如何基于布尔值筛选存在True观测值的accountID列表
Got it, let's work through this. You need to pull all accountIDs that have at least one 'T' (True) in any of the date-specific var1 columns, and since you're dealing with millions of rows, we'll prioritize efficient, scalable methods over slower row-wise operations.
First, let's confirm your sample data (formatted for readability):
df1 <- data.frame( accountID = c(1, 2, 3, 4, 5), var1_2018_1 = c('T', 'F', 'F', 'T', 'T'), var1_2018_2 = c('F', 'F', 'F', 'T', 'T'), var1_2018_3 = c('T', 'F', 'F', 'T', 'T'), var1_2018_4 = c('T', 'F', 'F', 'T', 'T'), var1_2018_5 = c('T', 'F', 'F', 'T', 'T'), var1_2018_6 = c('T', 'F', 'F', 'T', 'T'), var1_2018_7 = c('T', 'F', 'F', 'T', 'T'), var1_2018_8 = c('F', 'F', 'T', 'F', 'T'), var1_2018_9 = c('T', 'F', 'F', 'T', 'T'), var1_2018_10 = c('T', 'F', 'F', 'T', 'T'), var1_2018_11 = c('T', 'F', 'F', 'T', 'T'), var1_2018_12 = c('T', 'F', 'F', 'T', 'T') )
Method 1: Base R (Fastest for Large Datasets)
This uses native R vector operations, which are optimized for speed—perfect for million-row datasets.
# Identify all columns starting with "var1_" var_columns <- grep("^var1_", names(df1)) # Calculate the number of 'T's per row, filter rows with at least one, then extract accountID valid_accounts <- df1$accountID[rowSums(df1[, var_columns] == "T") > 0] # Output: [1] 1 3 4 5 print(valid_accounts)
Notes:
- If your data has
NAvalues, addna.rm = TRUEtorowSumsto avoid invalid results:valid_accounts <- df1$accountID[rowSums(df1[, var_columns] == "T", na.rm = TRUE) > 0]
Method 2: dplyr (Tidyverse-Friendly)
If you prefer the tidyverse syntax, use this vectorized approach (avoid rowwise() for large data—it's slower):
library(dplyr) valid_accounts <- df1 %>% # Keep only accountID and var1 columns select(accountID, starts_with("var1_")) %>% # Filter rows where at least one var1 column is 'T' filter(rowSums(select(., -accountID) == "T") > 0) %>% # Pull the accountID column as a vector pull(accountID) # Output: [1] 1 3 4 5 print(valid_accounts)
Why avoid rowwise()?
Row-wise operations iterate over every row individually, which becomes a bottleneck with millions of records. The rowSums approach is fully vectorized, so it processes the entire dataset in bulk—way faster.
Expected Result
For your sample data, the valid accountIDs are 1, 3, 4, 5 (account 2 has no 'T's in any var1 column).
内容的提问来源于stack exchange,提问作者TKTK

