You need to enable JavaScript to run this app. 基于purrr和dplyr从多份.rda文件中筛选指定列并保存
优惠活动
大模型
产品
解决方案
定价
更多

基于purrr和dplyr从多份.rda文件中筛选指定列并保存

Hey there! Let's fix this and get your .rda files processed smoothly using purrr and dplyr like you wanted. Here's a step-by-step solution tailored to your file structure:

1. First, Set Up Your Tools

Start by loading the tidyverse packages we'll need:

library(tidyverse)
2. Grab All Your .rda Files

First, point R to your target directory and collect paths for all .rda files:

# Replace this with your actual directory path
target_dir <- "path/to/your/rda/files"
rda_files <- list.files(path = target_dir, pattern = "\\.rda$", full.names = TRUE)
3. Build a Reusable Processing Function

This function will load each .rda file, filter the columns you need, and save the cleaned version. It handles the quirk of .rda files (they store named objects, not just raw data frames):

process_rda_file <- function(file_path) {
  # Load the file and capture the name of the object inside it
  loaded_obj_name <- load(file_path)
  raw_data <- get(loaded_obj_name)
  
  # Filter columns: keep first 28 columns + all columns starting with "SC"
  filtered_data <- raw_data %>%
    select(1:28, starts_with("SC"))
  
  # Create an output file name (adds "_filtered" to the original name)
  output_file <- str_replace(file_path, "\\.rda$", "_filtered.rda")
  
  # Save the filtered data with the same object name as the original file
  save(list = loaded_obj_name, file = output_file, envir = list2env(list(!!loaded_obj_name := filtered_data)))
  
  # Clean up temporary objects to keep your workspace tidy
  rm(raw_data, filtered_data, loaded_obj_name)
  invisible(output_file)
}

Quick breakdown of what this does:

  • load(file_path) returns the name of the object stored in the .rda file (most .rda files have one object, but this works if there's more)
  • select(1:28, starts_with("SC")) hits exactly your needs: keeps the first 28 fixed columns, plus any column starting with "SC" (regardless of their exact names)
  • We save the filtered data with the original object name so loading the cleaned file feels the same as the original
4. Batch Process All Files

Use purrr::walk() to run our function on every .rda file in your directory. We use walk() instead of map() because we just want to save files, not collect results:

# Process all files in one go
walk(rda_files, process_rda_file)
Troubleshooting Common Issues

If you ran into errors before, here are the most likely fixes:

  • "Object not found" error: If your .rda files have multiple objects, use this adjusted function to handle all of them:
    process_multi_obj_rda <- function(file_path) {
      loaded_obj_names <- load(file_path)
      temp_env <- new.env()
      load(file_path, envir = temp_env)
      
      # Filter columns for every object in the file
      filtered_env <- map(temp_env, ~ .x %>% select(1:28, starts_with("SC")))
      
      output_file <- str_replace(file_path, "\\.rda$", "_filtered.rda")
      save(list = names(filtered_env), file = output_file, envir = filtered_env)
      
      rm(temp_env, filtered_env, loaded_obj_names)
      invisible(output_file)
    }
    
  • "Can't subset columns that don't exist": Double-check that your data actually has 28 columns, or adjust the 1:28 to match your real column count. Also, if "SC" is case-sensitive (e.g., some columns start with "sc"), add ignore.case = TRUE to starts_with().
  • Permission errors: Make sure you have write access to the directory, or adjust output_file to save to a folder you control.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:11:47