0

我编写了以下代码将数据导入 R:

## specify where all the data files are stored 
DataFolder <- "DataFolder"

## obtain the name of each file in DataFolder
files <- list.files(DataFolder)

## obtain name of each file
LocNames <- unique(sub("^([^.]*).*", "\\1", files)) # this removes the extension and keeps the unique names

for (i in 1:length(LocNames)){

  #
  car <- read.table(paste(DataFolder, paste(LocNames[i], ".car", sep=""), sep="/"),
                    header = TRUE, sep = "\t", colClasses=c(dateTime="POSIXct"))  
  car <- aggregate(car[colnames(car)[2:length(colnames(car))]],list(dateTime = cut(car$dateTime,breaks = "hour")),mean, na.rm = TRUE)

  #  
  light <- read.table(paste(DataFolder, paste(LocNames[i], ".light", sep=""), sep="/"),
                    header = TRUE, sep = "\t", colClasses=c(dateTime="POSIXct"))
  light <- aggregate(light[colnames(light)[2]],list(dateTime = cut(light$dateTime, breaks = "hour")),mean, na.rm = TRUE)
}

所以,在这里我有一个 DataFolder,我的所有文件都存储在其中。这些文件是根据记录数据的位置和文件的扩展名命名的,该文件的扩展名是被测变量的名称。这里我们以汽车销售和灯光为例。

从这里开始,我想减少循环内参数的大小,因此不必在另一个变量之后重复相同的步骤命名一个变量,我只想写变量名,例如汽车、灯光,然后是结果显示的脚本将被返回。

如果我的意图不明确,请告诉我。

4

1 回答 1

1

只需使用一个功能。大意是什么

## specify where all the data files are stored 
DataFolder <- "DataFolder"

## obtain the name of each file in DataFolder
files <- list.files(DataFolder)

readMyFiles <- function(DataFolder, LocNames, extension){
  data <- read.table(paste(DataFolder, paste(LocNames[i], ".", extension, sep=""), sep="/"),
                    header = TRUE, sep = "\t", colClasses=c(dateTime="POSIXct"))  
  data <- aggregate(data[colnames(data)[2:length(colnames(data))]],list(dateTime = cut(data$dateTime,breaks = "hour")),mean, na.rm = TRUE)
  data
}

## obtain name of each file
LocNames <- unique(sub("^([^.]*).*", "\\1", files)) # this removes the extension and keeps the unique names

for (i in 1:length(LocNames)){

  car <- readMyFiles(DataFolder, LocNames, ".car")

  light <- readMyFiles(DataFolder, LocNames, ".light")
}
于 2013-04-25T18:52:10.517 回答