简体   繁体   English

无法读取CSV档案

[英]Cannot Read csv file

I am new to R and I have this proplem: I have a set of csv files, each have 3 colums with numeric values. 我是R的新手,但我有一个问题:我有一组csv文件,每个文件都有3个带有数字值的列。 I am traying to run a set of instructions to create categories that allow me to align the graphs of each of the files. 我正在运行一组说明来创建类别,这些类别使我可以对齐每个文件的图形。 This is so far my set of commands: 到目前为止,这是我的命令集:

myFiles<-dir("C:\\Data\\")
myDeadVols<-as.matrix(read.csv("C:\\Data\\Dead Volumes.csv", header=T, sep="|", row.names=1))

myDeadVolsVec <- as.numeric(myDeadVols[,"t2_min"])*60 + as.numeric(myDeadVols[,"t2_s"])

names(myDeadVolsVec) <- myDeadVols[,"fileName"]

sampAnot <- names(myDeadVolsVec)

names(sampAnot) <- sampAnot

polyRead <- function(fileNames=NULL, mySep="|"){

dataList <- list()

for(tmpName in fileNames){dataList[[tmpName]] <-as.matrix(read.table(tmpName, header=TRUE, sep=mySep))}
return(dataList)}

polyReadMaritza <- function(fileNames=NULL, mySep="|", file2void=NA, fracTime=35, dataPerFrac=173){

dataList <- list()
for(tmpName in fileNames){
tmpMat<-as.matrix(read.table(tmpName, header=FALSE, sep=mySep))
tmpVoidTime<-file2void[tmpName]
tmpVoidPoints<-tmpVoidTime/fracTime*dataPerFrac
tmpAdder<-matrix(ncol=3, nrow=as.integer(tmpVoidPoints), data=0)
tmpMat<-rbind(tmpAdder, tmpMat)
tmpEvent<-rep(0, dim(tmpMat)[1])
fractMoves<-c(1:length(tmpEvent))[which(c(1:length(tmpEvent))%%dataPerFrac==0)]
tmpEvent[fractMoves]<-1
tmpMat[,3]<-tmpEvent
dataList[[tmpName]] <- tmpMat
colnames(dataList[[tmpName]])<-c("Distance.mm.", "Absorbance", "Event")}
return(dataList)}

So far everything seems ok but when I try to enter the fllowin command: 到目前为止,一切似乎还不错,但是当我尝试输入fllowin命令时:

myDataList <- polyReadMaritza(myFiles, mySep="\t", file2void=myDeadVolsVec)

I have this error: 我有这个错误:

Error in file(file, "rt") : cannot open the connection. file(file,“ rt”)中的错误:无法打开连接。 In addition: Warning message:In file(file, "rt") : cannot open file 'Beads.csv': No such file or directory 另外:警告消息:在文件(文件,“ rt”)中:无法打开文件'Beads.csv':没有此类文件或目录

Now, the path to 'Beads.csv' is C:\\Data\\Beads.csv, so I think that is because It does not read the complet path to 'Beads.csv' (which is the first of the files) but I thought that with "myFiles<-dir("C:\\Data\\")" I already specified the path. 现在,“ Beads.csv”的路径为C:\\ Data \\ Beads.csv,所以我认为这是因为它没有读取“ Beads.csv”的完整路径(这是文件的第一个),但是我认为使用“ myFiles <-dir(“ C:\\ Data \\”)“,我已经指定了路径。 Any help is welcomed 欢迎任何帮助

You have actually get the list of files in the desired directory. 您实际上已经获得了所需目录中的文件列表。

fileDir = 'C:/Data'
myFiles = list.files(path=fileDir, pattern='*.csv')

Then when you call read.csv you have to give R the full filepath with... 然后,当您调用read.csv您必须使用以下命令为R提供完整的文件路径:

for(file in myFiles){
    read.csv(file.path(fileDir,file))
}

The value that was returned by dir did not include paths. dir返回的值不包含路径。 The default for "full.names" in dir is FALSE so unless your working directory is 'c://Data//' the read.csv call will not be looking in the correct location. dir “ full.names”的默认值为FALSE,因此,除非您的工作目录为“ c:// Data //”, read.csv调用不会在正确的位置查找。 Try: 尝试:

myFiles <- dir("C:\\Data\\", pattern= "[.]csv", full.names-TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM