如何用R语言将相同键对应的值合并至同一行?
Got it, let's figure out how to reformat that string exactly as you want using R. I'll show you two approaches—one using the tidyverse (great for readability and straightforward grouping) and a base R version if you prefer not to load extra packages.
Approach 1: Using Tidyverse Tools
First, we'll use stringr for string manipulation and dplyr for grouping. If you haven't installed the tidyverse yet, run the install command first:
# Install tidyverse if you haven't already install.packages("tidyverse") library(tidyverse)
Now let's walk through the steps:
- Define your original string:
original_str <- "Wood Tabletops RE170590 Wood Tabletops RE170590 Wood Tabletops RE170590 Wood Tabletops RE170590 Wood Tabletops RE170590 Wood Tabletops RE170590 Watertap RE170584 Water Heater RE170584"
- Extract each individual "Product Name + ID" entry. We'll use a regex to grab each entry, even if the product name has multiple words:
# Extract every "Name + ID" pair using non-greedy matching entries <- str_extract_all(original_str, ".*? RE\\d+")[[1]]
The regex .*? RE\\d+ works by matching characters until it hits the RE prefix followed by numbers (your product ID), ensuring we capture full names like "Wood Tabletops" instead of splitting mid-name.
- Split each entry into separate name and ID columns:
# Split entries into name and ID, then clean up the ID format entry_df <- str_split_fixed(entries, " RE", n = 2) %>% as.data.frame(stringsAsFactors = FALSE) %>% rename(product_name = V1, product_id = V2) %>% mutate(product_id = paste0("RE", product_id)) # Add back the "RE" to the ID
- Group by product ID and collapse the names with commas, then combine groups with semicolons:
# Group by ID, collapse names, then combine groups final_result <- entry_df %>% group_by(product_id) %>% summarise(collapsed_names = paste(product_name, collapse = ", ")) %>% pull(collapsed_names) %>% paste(collapse = "; ")
- Print the result:
print(final_result)
This will output exactly what you want:
"Wood Tabletops, Wood Tabletops, Wood Tabletops, Wood Tabletops, Wood Tabletops, Wood Tabletops; Watertap, Water Heater"
Approach 2: Base R (No Extra Packages)
If you prefer sticking to base R, here's an equivalent workflow:
original_str <- "Wood Tabletops RE170590 Wood Tabletops RE170590 Wood Tabletops RE170590 Wood Tabletops RE170590 Wood Tabletops RE170590 Wood Tabletops RE170590 Watertap RE170584 Water Heater RE170584" # Extract all "Name + ID" entries entries <- regmatches(original_str, gregexpr(".*? RE\\d+", original_str))[[1]] # Split each entry into name and ID split_entries <- strsplit(entries, " RE") product_names <- sapply(split_entries, function(x) x[1]) product_ids <- paste0("RE", sapply(split_entries, function(x) x[2])) # Group names by ID and collapse them grouped_names <- tapply(product_names, product_ids, function(x) paste(x, collapse = ", ")) # Combine groups with semicolons final_result <- paste(grouped_names, collapse = "; ") # Output the result print(final_result)
Both methods will give you the formatted string you need. The tidyverse approach is more readable for complex workflows, while base R is great if you want to avoid package dependencies.
内容的提问来源于stack exchange,提问作者Viskete

