如何在R中读取和命名不同的CSV文件 [英] How to read and name different CSV files in R
问题描述
我想在多个csv文件上进行比较,所以我编写了这段代码来读取我拥有的不同csv文件:
I would like to work on several csv files to make some comparisons, so I wrote this code to read the different csv files I have:
path <- "C:\\data\\"
files <- list.files(path=path, pattern="*.csv")
for(file in files)
{
perpos <- which(strsplit(file, "")[[1]]==".")
assign(
gsub(" ","",substr(file, 1, perpos-1)),
read.csv(paste(path,file,sep="")))
}
我的csv文件是这样的:
My csv files are something like this:
Start Time,End Time,Total,Diffuse,Direct,Reflected
04/09/14 00:01:00,04/09/14 00:01:00,2.221220E-003,5.797364E-004,0.000000E+000,1.641484E-003,
04/09/14 00:02:00,04/09/14 00:02:00,2.221220E-003,5.797364E-004,0.000000E+000,1.641484E-003,
04/09/14 00:03:00,04/09/14 00:03:00,2.221220E-003,5.797364E-004,0.000000E+000,1.641484E-003,
(...)
使用我的代码,R正确分离所有文件,但要分别em会在表的开头添加一个额外的空间:
Using my code, R separate correctly all files, but for each of them it creates a table adding a more extra space at the beginning:
|Start Time |End Time |Total |Diffuse |Direct |Reflected
04/09/14 00:01:00|04/09/14 00:01:00|2.221220E-003|5.797364E-004|0.000000E+000|1.641484E-003|NA
...
我该如何解决?
此外,考虑到每个文件的原始名称确实很长,是否可以使用文件的最后一个字母来命名每个data.frame?还是只是基数?
Moreover, considering that the original name of each file is really long, is it possible to name each data.frame using the last letters of the file? Or just a cardinal number?
推荐答案
我建议使用data.table包-速度更快,并且用于非空白列最后,它将这些转换为 NA
(以我的经验)。这是我为类似任务编写的一些代码:
I would suggest using the data.table package - it's faster and for non-blank columns in the end, it converts those to NA
(in my experience). Here's some code I wrote for a simialr task:
read_func <- function(z) {
dat <- fread(z, stringsAsFactors = FALSE)
names(dat) <- c("start_time", "end_time", "Total", "Diffuse", "Direct", "Reflect")
dat$start_tme <- as.POSIXct(strptime(dat$start_tme,
format = "%d/%m/%y %H:%M:%S"), tz = "Pacific/Easter")
patrn <- "([0-9][0-9][0-9])\\.csv"
dat$type <- paste("Dataset",gsub(".csv", "", regmatches(z,regexpr(patrn, z))),sep="")
return(as.data.table(dat))
}
path <- ".//Data/"
file_list <- dir(path, pattern = "csv")
file_names <- unname(sapply(file_list, function(x) paste(path, x, sep = "")))
data_list <- lapply(file_names, read_func)
dat <- rbindlist(data_list, use.names = TRUE)
rm(path, file_list, file_names)
带有每个项目的列表作为cor中的data.table响应文件名。我假设所有文件名在扩展名前都有一个三位数,我曾用它为每个data.table分配一个变量 type
。您可以更改 patrn
以匹配您的特定用例。这样,当您将它们全部合并到单个data.table dat
中时,始终可以根据类型进行排序/过滤。例如,如果您想绘制 Dataset158
diffuse vs direct
>和 datase222
,您可以执行以下操作:
This will give you a list with each item as the data.table from the corresponding file name. I assumed that all file names have a three digit number before the extension which I used to assign a variable type
to each data.table. You can change patrn
to match your specific use case. This way, when you combine all of them into a single data.table dat
, you can always sort/filter based on type. For example, if youwanted to plot diffuse
vs direct
for Dataset158
and datase222
, you could do the following:
ggplot(data = dat[type == 'Dataset158' | type == 'Dataset222'],
aes(x = Diffuse, y = Direct)) + geom_point()
希望这会有所帮助!
这篇关于如何在R中读取和命名不同的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!