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

R语言向量按正则交替模式补全:填充缺失#字符的需求

Solution: Fill Missing # Items in Alternating R Vector

Got it, let's solve this problem where we need to fill gaps in a vector that's supposed to alternate between pure alphabetic strings and #-followed-by-digits strings. The gaps occur when two letters are consecutive, or when the vector ends with a letter (needing a final # item).

Approach

Here's the plan:

  1. Classify Elements: First, we'll categorize each element as either a "letter" (pure alphabetic) or "hash" (starts with # and followed by digits).
  2. Generate New Hash Values: We'll use the maximum number from existing hash items to generate sequential new hash values (so we don't repeat numbers).
  3. Iterate and Fill Gaps: Go through the vector, inserting a new hash item whenever two consecutive elements are the same type (specifically two letters in a row).
  4. Final Check: If the last element is a letter, append a final hash item to complete the alternating pattern.

Complete Function

fill_hash_gaps <- function(vec) {
  # Handle empty vector case
  if (length(vec) == 0) return(vec)
  
  # Helper to determine element type
  get_type <- function(x) {
    if (grepl("^#\\d+$", x)) {
      "hash"
    } else if (grepl("^[a-zA-Z]+$", x)) {
      "letter"
    } else {
      stop(paste("Unrecognized element:", x, "\nMust be pure letters or # followed by digits."))
    }
  }
  
  # Extract existing hash numbers to generate sequential new ones
  hash_elements <- vec[grepl("^#\\d+$", vec)]
  hash_nums <- as.integer(sub("^#", "", hash_elements))
  current_hash_num <- ifelse(length(hash_nums) == 0, 1, max(hash_nums) + 1)
  
  # Initialize result with first element
  result <- vec[1]
  last_type <- get_type(result)
  
  # Process remaining elements
  for (elem in vec[-1]) {
    current_type <- get_type(elem)
    
    # Insert hash if current element matches last type (two letters in a row)
    if (current_type == last_type) {
      new_hash <- paste0("#", current_hash_num)
      result <- c(result, new_hash)
      current_hash_num <- current_hash_num + 1
      last_type <- "hash" # Update last type to hash after insertion
    }
    
    # Add the current element to result
    result <- c(result, elem)
    last_type <- current_type
  }
  
  # Add final hash if last element is a letter
  if (last_type == "letter") {
    new_hash <- paste0("#", current_hash_num)
    result <- c(result, new_hash)
  }
  
  return(result)
}

Test with Your Example

Let's apply this function to your sample vector:

vec <- c("ab", "#4", "gw", "#29", "mp", "jq", "#35", "ez")
filled_vec <- fill_hash_gaps(vec)
print(filled_vec)

Output:

[1] "ab"  "#4"  "gw"  "#29" "mp"  "#36" "jq"  "#35" "ez"  "#37"

Explanation of the Result

  • Between mp and jq (two consecutive letters), we inserted #36 (next sequential number after the max existing hash value 35).
  • After ez (ends with a letter), we added #37 to complete the alternating pattern.
  • The resulting vector strictly alternates between letter and hash elements as required.

Notes

  • The function assumes all input elements are either pure letters or # followed by digits (it throws an error if it encounters an unrecognized element).
  • New hash values are generated sequentially from the highest existing hash number to avoid duplicates.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:02:57