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

带年份条件统计列中字符串在另一列的出现次数(R语言)

Solution for Counting Animal Occurrences Within 5-Year Window

Let's break down how to solve this problem—we need to count how many times each animal from animals.1 appears in animals.2 entries from the current row's year and the 5 years prior. Here's a step-by-step implementation using dplyr and tidyr for clarity:

Step 1: Prepare the Data

First, let's confirm our input data (same as your example):

df = data.frame(
  animals.1 = c("cat; dog; bird", "dog; bird", "bird", "dog"), 
  animals.2 = c("cat; dog; bird","dog; bird; seal", "bird", ""),
  year= c("2001","2005","2010","2018"), 
  stringsAsFactors = F
)

Step 2: Full Implementation Code

We'll use row-wise processing to handle each row's unique 5-year window, then split and count animal occurrences:

library(dplyr)
library(tidyr)

# Convert year to numeric for time window calculations
df <- df %>% mutate(year = as.numeric(year))

# Calculate the count for each row
df <- df %>%
  rowwise() %>%
  mutate(
    # Define the 5-year window (current year minus 5 to current year)
    window_start = year - 5,
    # Filter all rows that fall within this window
    filtered_rows = list(filter(df, year >= window_start & year <= year)),
    # Split animals.1 into individual animal entries
    target_animals = strsplit(animals.1, "; ")[[1]],
    # Count how many times each target animal appears in filtered animals.2 entries
    count = sum(sapply(target_animals, function(animal) {
      # Split all filtered animals.2 into individual animals and count matches
      sum(strsplit(filtered_rows$animals.2, "; ") %>% 
            unlist() %>% 
            grepl(paste0("^", animal, "$"), .))
    }))
  ) %>%
  # Clean up helper columns
  select(-window_start, -filtered_rows, -target_animals)

# View the final result
df

Step 3: Verify the Output

Running this code will produce exactly the expected result:

animals.1       animals.2  year count
1 cat; dog; bird cat; dog; bird 2001     3
2       dog; bird dog; bird; seal 2005     4
3           bird           bird 2010     1
4            dog                2018     0

Key Explanations

  • rowwise(): Ensures we process each row independently, so each row's 5-year window is calculated based on its own year value.
  • Time Window Filter: filtered_rows captures all rows where the year is between current year -5 and current year (inclusive).
  • String Splitting: strsplit breaks down the semicolon-separated animal strings into individual entries, making it easy to count matches.
  • Match Counting: The inner sapply loops through each animal from animals.1, counts how many times it appears in all filtered animals.2 entries, and sums those counts to get the final count for the row.

For larger datasets, you might want to optimize this by converting the data to a long format first (using separate_rows), but this row-wise approach is straightforward and works well for most use cases.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 03:48:38