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:
- Classify Elements: First, we'll categorize each element as either a "letter" (pure alphabetic) or "hash" (starts with
#and followed by digits). - 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).
- 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).
- 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
mpandjq(two consecutive letters), we inserted#36(next sequential number after the max existing hash value 35). - After
ez(ends with a letter), we added#37to 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
相关产品推荐
相关产品推荐

