R for循环仅适用于一个文件 [英] R for loop works only for one file

查看:182
本文介绍了R for循环仅适用于一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为R中的多个csv文件运行一个for循环,但循环只运行第一个文件。



我想导入csv文件然后为每个将存储数据分析的csv文件创建一个目录。创建目录后,我有问题,每次运行我的代码时将其设置为工作目录。我的代码工作正常,当它只是一个文件,但它失败时,我使用for循环。

代码:



<设置工作目录和路径
setwd(path)
path< - path

##从每个路径提取文件名
files< - list.files(path = path,pattern =* .csv)
(文件在文件中)
{
temp< - which(strsplit(file,)[[1]] ==。)
assign(
gsub(,,substr(file,1,temp- 1)),
read.csv(粘贴(路径,文件,sep =)))
}

##为每个文件创建一个新目录并设置那作为新的工作目录。

for(se in(1,length(files),by = 1)){
fileName< - read.csv(files [i])
base< ; - strsplit(files [i],.csv)[[1]]
dir< - dir.create(paste(path,base,sep =/))
setwd getwd(dir))



进一步分析存储在新设置的工作目录中的结果。


$ b $ h1创建变量

  Date_Time<  -  strptime(fileName $ Date ... Time ,format =%d /%m /%Y%H:%M)
fileName $ month < - months(Date_Time,abbreviate = TRUE)#creates month column(char)
fileName $ julian< - Date_Time $ yday #creates julian day column
fileName $ year< - Date_Time $ year + 1900 #creates year column
fileName $ hour< - Date_Time $ hour #creates hour column
fileName $ weeknum< - round(Date_Time $ yday / 7,0)
fileName $ numericdate< - fileName $ year + fileName $ julian / 366#日期的数值

#Identify并删除空列
fi leName< - as.data.table(fileName)
fileName< - fileName [,which(unlist(lapply(fileName,function(x)!all(is.na(x))))),with = f]
dim(fileName)#检查空列是否被清除
head(fileName)#为PM10数据找到合适的列名
PM10 < - fileName $ PM10_BAM #substitue用于进一步计算的公共变量
fileName $ PM10_BAM< - as.numeric(as.character(PM10))

##通过数据
查看基本季节模式df_eve< - subset(fileName,hour> = 18)
jpeg(file =evenings.jpg中观察到的季节性模式)
(df_eve,boxplot(PM10_BAM〜weeknum,main =seasonal在晚上观察到的模式,xlab =weeknum,ylab =PM10,outline = FALSE,na.rm = T))
dev.off()
}


错误:

文件错误(文件rt):无法打开连接另外:警告信息:
1:在dir.create(paste路径,base,sep =/)):
'/ Users / ayushikach hara / Desktop / Work / CSV // EW_Matamata'已经存在
2:由强制引入的NA
3:在文件(file,rt)中:
无法打开文件'EW_Ngaruawahia.csv ':没有这样的文件或目录

EW_Matamata和EW_Ngaruawahia是最初设置的工作目录中的文件。但是,因为我导入他们,然后改变目录,我不明白为什么我不断收到第三个错误信息。



任何帮助表示赞赏,因为我在学习阶段:)

解决方案



<$ (bir)(
$> $ code> dir < - dir.create(paste(path,base,sep =/))
setwd(getwd(dir))

现在,当它获得第一个文件时,它将创建一个新目录并将工作目录设置为新创建的目录。在当前目录中寻找第二个文件,显然它不在那里

I have been trying to run a for loop for multiple csv files in R. But the loop only runs for the first file.

I want to import csv files and then create a directory for each csv file where the analysis of its data will be stored. After creating directories, I'm having problem with setting that as a working directory each time I run my code. My code works fine when it is just one file but it fails when I use the for loops.

Code:

## Setting the working directory and path
setwd("path")
path <- "path"

##to extract the filename from each path
files <- list.files(path=path, pattern="*.csv")
for(file in files)
{
  temp <- which(strsplit(file, "")[[1]]==".")
  assign(
    gsub(" ","",substr(file, 1, temp-1)), 
    read.csv(paste(path,file,sep="")))
}

##To create a new directory for each file and set that as the new working directory.

    for(i in seq(1, length(files), by = 1)){
      fileName <- read.csv(files[i])
      base <- strsplit(files[i], ".csv")[[1]]
      dir <- dir.create(paste(path,base, sep = "/"))
      setwd(getwd(dir))

Further analysis with results stored in the newly set working directory.

Creating variables

Date_Time <- strptime(fileName$Date...Time, format = "%d/%m/%Y %H:%M")
  fileName$month <- months(Date_Time,abbreviate = TRUE)  #creates month column (char)
  fileName$julian <- Date_Time$yday  #creates julian day column
  fileName$year <- Date_Time$year + 1900  #creates year column
  fileName$hour <- Date_Time$hour  #creates hour column
  fileName$weeknum <- round(Date_Time$yday/7,0)
  fileName$numericdate <- fileName$year+fileName$julian/366  #numeric value of date

  #Identify and remove empty columns
  fileName <- as.data.table(fileName)
  fileName <- fileName[,which(unlist(lapply(fileName, function(x)!all(is.na(x))))),with=F]
  dim(fileName) # to check if empty columns have been eliminated
  head(fileName) #to find appropriate column name for PM10 data
  PM10 <- fileName$PM10_BAM #substitue in a common variable for further calculations
  fileName$PM10_BAM <- as.numeric(as.character(PM10))

  ##to view basic seasonal pattern through the data
  df_eve <- subset(fileName, hour>=18)
  jpeg(file = "seasonal pattern observed in the evenings.jpg")
  with(df_eve, boxplot(PM10_BAM ~ weeknum, main = "seasonal pattern observed in the evenings", xlab = "weeknum", ylab = "PM10", outline = FALSE, na.rm = T))
  dev.off()
}


Errors:

Error in file(file, "rt") : cannot open the connection In addition: Warning messages:
1: In dir.create(paste(path, base, sep = "/")) :
  '/Users/ayushikachhara/Desktop/Work/CSV//EW_Matamata' already exists
2: NAs introduced by coercion 
3: In file(file, "rt") :
  cannot open file 'EW_Ngaruawahia.csv': No such file or directory

EW_Matamata and EW_Ngaruawahia are the files in the initially set working directory. But since I import them and then change the directory, I don't understand why I keep getting the 3rd error message.

Any help is appreciated since I'm at a learning stage :)

解决方案

Check this line of your code .

      dir <- dir.create(paste(path,base, sep = "/"))
      setwd(getwd(dir))

Now when it gets the first file it creates a New Dir and sets the working dir as the Directory of the newly created Dir .So when it looks for the second file in the current Dir ,its Obviously is not present there

这篇关于R for循环仅适用于一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆