[英]R, R Programming, Loop Directory
Hi I am trying to loop through a directory of excel files for analysis. 嗨,我正在尝试遍历excel文件目录进行分析。
My Variable is named FileToGrab which obtains the name of the excel file. 我的变量名为FileToGrab,它获取excel文件的名称。
Where I have FileToGrab in bold is what I want the data frame to be named not the actual FileToGrab data frame. 我要用粗体显示FileToGrab的地方是我希望将数据框架命名为实际的FileToGrab数据框架。
Example FileToGrab = 2013ExcelSheet23 示例FileToGrab = 2013ExcelSheet23
I want my Data Frame to be named 2013ExcelSheet23 and not FileToGrab. 我希望将数据框命名为2013ExcelSheet23,而不是FileToGrab。
FileToGrab = 2013ExcelSheet24 FileToGrab = 2013ExcelSheet24
I want my Data Frame to be named 2013ExcelSheet24 and not FileToGrab. 我希望将数据框命名为2013ExcelSheet24,而不是FileToGrab。
FileToGrab = 2013ExcelSheet25 FileToGrab = 2013ExcelSheet25
I want my Data Frame to be named 2013ExcelSheet25 and not FileToGrab. 我希望将数据框命名为2013ExcelSheet25,而不是FileToGrab。
..... and so on. ..... 等等。
New to R sorry if this does not make sense. R的新手,对不起,如果这没有道理。 Thanks 谢谢
x <- 1:50
for(i in seq(along=x))
{
FileToGrab = gsub("(^ +)|( +$)", "",listofFile[i])
FileToGrab = str_replace_all(string=FileToGrab, pattern=" ", repl="")
DirFileName = paste("C:\\Users\\w47593\\Desktop\\RProjects\\CallCenterProjectJuly2013\\Files\\",FileToGrab)
DirFileName = str_replace_all(string=DirFileName, pattern=" ", repl="")
file.name <- DirFileName
sheet.name <- "Detail"
FileToGrab = str_replace_all(string=FileToGrab, pattern=".xls", repl="")
## Connect to Excel File Pull and Format Data
excel.connect <- odbcConnectExcel(DirFileName)
**FileToGrab** <- sqlFetch(excel.connect, sheet.name, na.strings=c("","-"))
odbcClose(excel.connect)
}
You probably don't want to name your objects starting with numbers as you would have to quote them each time you used them 你可能不希望把你的对象开始用数字,你将有你使用他们每次说出来了
> 11Foo <- 1
Error: unexpected symbol in "11Foo"
> `11Foo` <- 1
> 11Foo
Error: unexpected symbol in "11Foo"
> `11Foo`
[1] 1
Like wise, I doubt you want 25+ objects clogging up your workspace. 同样,我怀疑您是否希望25个以上的对象阻塞您的工作空间。 A far better solution is often to import the data into a list and work with those objects. 更好的解决方案通常是将数据导入列表并使用这些对象。 You have similar issues with accessing the names 您在访问名称时遇到类似的问题
> ll <- list(`1` = 1, `2` = 2)
> ll$1
Error: unexpected numeric constant in "ll$1"
> ll$`1`
[1] 1
but then you don't need to refer to them by name necessarily, and you benefit by being able to iterate over the list using lapply
etc. 但是您不必一定要使用名称来引用它们,并且可以使用lapply
等在列表中进行迭代,从而从中受益。
I would use something like 我会用类似的东西
fs <- list.file("dir/to/excel/files", glob2rx("*.xls"))
ll <- vector(mode = "list", length = length(fs))
for (i in seq_along(ll)) {
excel.connect <- odbcConnectExcel(fs[i])
ll[[i]] <- sqlFetch(excel.connect, sheet.name, na.strings=c("","-"))
odbcClose(excel.connect)
}
names(ll) <- sub("\\.xls", "", fs)
You would still have to extract via 您仍然必须通过提取
ll$"2013ExcelSheet25"
but you can also use 但您也可以使用
ll[["2013ExcelSheet25"]]
or better 或更好
ll[[1]]
or even 甚至
ll[[which(names(ll) == "2013ExcelSheet25")]]
But because these are all in a single list the are contained, and you can operate on them via lapply
and co. 但是因为它们全部包含在一个列表中,所以您可以通过lapply
和co对它们进行操作。
Why not use 为什么不使用
files = list.files(DirFileName)
and then iterate through that to load them into R? 然后遍历,将它们加载到R中?
Assignment to object using filenames: 使用文件名分配给对象:
objects = list()
objects[[files[1]]] = ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.