You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于布尔值筛选存在True观测值的accountID列表

Solution to Filter accountIDs with at Least One 'T' in Any var1 Column

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 NA values, add na.rm = TRUE to rowSums to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 22:57:31