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

如何在R中基于字符串列表条件创建DataFrame的active变量?

Efficiently Create 'active' Column in R DataFrame

Hey there! I totally get wanting an efficient solution for large datasets—looping or merging can get really slow, so let's go with a vectorized approach that's both fast and straightforward.

The Core Solution

We can use R's built-in vectorized operations to check if either screen_name or rt_name exists in your act_users list, then convert the logical result to integers (1 for TRUE, 0 for FALSE). Here's how:

# Your sample data
df <- data.frame(
  screen_name = c("august", "berit", "christopher", "david", "erica", "frank"),
  rt_name = c("berit", "august", "david", "erica", "frank", "christopher")
)
act_users <- c("david", "august", "berit")

# Create the 'active' column efficiently
df$active <- as.integer(df$screen_name %in% act_users | df$rt_name %in% act_users)

Why This Works (and Is Fast)

  • %in% is a vectorized function that checks each element of a column against your act_users list in one go—way faster than looping through rows.
  • The | operator performs a logical OR across the two columns' results: if either column matches, it returns TRUE.
  • as.integer() converts TRUE to 1 and FALSE to 0, which gives you exactly the binary value you need.

Let's Verify the Result

If you print the updated dataframe, you'll see the correct values for active:

print(df)
#   screen_name     rt_name active
# 1      august       berit      1
# 2       berit      august      1
# 3 christopher       david      1
# 4       david       erica      1
# 5        erica       frank      0
# 6       frank christopher      0

This approach avoids the overhead of merging dataframes or custom functions, making it perfect for large datasets where speed matters.

内容的提问来源于stack exchange,提问作者Mette

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:11:43