在R循环中批量格式化HOBOware数据CSV文件遇问题求助
Hey Chris, let's break down what's going wrong and get your batch processing working smoothly!
First, the Root Cause of That Error
You hit a classic mix-up between base R and readr package functions:
- Your single-file code uses
read_csv()(from thereadrpackage), which supports thecol_typesargument to define column formats. - But in your loop, you switched to
read.csv()(base R's CSV reader), which doesn't recognizecol_types—that's exactly why you're seeing that "unused argument" error.
Fix #1: Correct the Loop with read_csv
Let's rewrite the loop to match your working single-file code, plus add a way to store all your datasets instead of overwriting them each time:
# First, make sure you load the readr package (critical!) library(readr) # Set up your paths setwd("E:/R Hobo/Conversion test/Converted HOBO files") mydir = "Pool 6" myfiles = list.files(path = mydir, pattern = "*.csv", full.names = TRUE) # Create an empty list to hold each month's data pool_data <- list() # Loop through each file for (file_index in seq_along(myfiles)) { # Read the file with the same logic as your single-file code current_month <- read_csv( myfiles[file_index], col_types = cols(`Date Time, GMT-05:00` = col_datetime(format = "%m/%d/%y %H:%M:%S")), skip = 1 )[, 2:4] # Name the list entry after the file (so you can track which month is which) names(pool_data)[file_index] <- basename(myfiles[file_index]) # Add the data to the list pool_data[[file_index]] <- current_month } # Optional: Combine all data into one big data frame pool_combined <- do.call(rbind, pool_data)
Fix #2: Using lapply (Works in R 3.5.3!)
You mentioned hearing about lapply but thought it was incompatible with R 3.5.3—don't worry, lapply is a core base R function that works in every version! The issue was likely missing the readr package or a syntax misstep. Here's a clean lapply version:
library(readr) setwd("E:/R Hobo/Conversion test/Converted HOBO files") mydir = "Pool 6" myfiles = list.files(path = mydir, pattern = "*.csv", full.names = TRUE) # Read all files at once with lapply pool_data <- lapply(myfiles, function(file_path) { read_csv( file_path, col_types = cols(`Date Time, GMT-05:00` = col_datetime(format = "%m/%d/%y %H:%M:%S")), skip = 1 )[, 2:4] }) # Name the list elements for clarity names(pool_data) <- basename(myfiles) # Combine into a single data frame if needed pool_combined <- do.call(rbind, pool_data)
Quick Note for R 3.5.3 Compatibility
If you run into issues with readr, make sure you're using a version that supports R 3.5.3. The latest compatible version is readr 1.3.1—install it with:
install.packages("readr", version = "1.3.1")
内容的提问来源于stack exchange,提问作者Chris McNabb

