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

如何使用paste粘贴特定名称?寻求替代rbind批量处理G编号的方法

Better Ways to Combine Specific Named Objects in R

Hey there! I totally get where you're coming from—typing out rbind(G07, G08, G09, ...) over and over is tedious and messy, especially when you have a specific set of objects to work with. Let's look at a few cleaner, more scalable approaches to solve this:

1. Base R: Use mget() + do.call(rbind, ...)

This is a straightforward base R solution that avoids manually listing every object in rbind():

# First, define a vector with all your target object names
target_objects <- c("G07", "G08", "G09", "G11", "G12", "G13", "G14")

# Fetch all objects into a list, then bind them row-wise
combined_df <- do.call(rbind, mget(target_objects))

mget() grabs all the objects matching your name vector and returns them as a list, then do.call(rbind, ...) applies rbind() to every element in the list in one go. No more repetitive typing!

2. Tidyverse: Use purrr::map_dfr()

If you're working in the tidyverse ecosystem, this approach is even more readable:

library(purrr)

target_objects <- c("G07", "G08", "G09", "G11", "G12", "G13", "G14")
combined_df <- map_dfr(target_objects, get)

map_dfr() iterates over your name vector, uses get() to retrieve each object, and automatically binds everything into a single data frame (the _dfr suffix stands for "data frame, row-wise").

If you also use dplyr, you can pair this with bind_rows() for more flexibility (like handling mismatched column names by filling in NAs):

library(dplyr)
library(purrr)

combined_df <- target_objects %>% 
  map(get) %>% 
  bind_rows()

3. Pro Tip: Store Objects in a List From the Start

If you haven't already created these objects, the best practice is to store them in a list instead of as separate global variables. This makes combining (and any future manipulation) way easier:

# When creating your data, store directly in a list
my_data <- list(
  G07 = your_G07_data,  # Replace with your actual data creation code
  G08 = your_G08_data,
  G09 = your_G09_data,
  G11 = your_G11_data,
  G12 = your_G12_data,
  G13 = your_G13_data,
  G14 = your_G14_data
)

# Now combining is as simple as:
combined_df <- bind_rows(my_data)  # or do.call(rbind, my_data) for base R

This avoids cluttering your global environment with multiple similar variables and keeps your workflow organized.

All these methods are way more maintainable than manually writing out rbind() for each object—especially if you ever need to add or remove objects from your list later!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:25:02