基于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:
Start by loading the tidyverse packages we'll need:
library(tidyverse)
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)
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
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)
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:28to match your real column count. Also, if "SC" is case-sensitive (e.g., some columns start with "sc"), addignore.case = TRUEtostarts_with(). - Permission errors: Make sure you have write access to the directory, or adjust
output_fileto save to a folder you control.
内容的提问来源于stack exchange,提问作者Scipione Sarlo

