我想创建一个函数来读取本地目录的所有csv,并将它们保存为R环境中的数据帧列表。我试着这样做:
getdflist <- function(directory = getwd()) {
setwd(directory)
monitors <- list.files(directory)
dataframes <- vector("list", length(monitors))
for (i in seq_along(monitors)) {
dataframes[[i]] <- read.csv(monitors[[i]])
print(i)
}
dataframes
}但它只打印i,并不将列表保存在我的环境中
有人能帮我看看我的代码中的错误吗?
注意:当我尝试将其作为代码(而不是函数)运行时:
> monitors <-list.files(getwd())
> dataframes <- vector("list", length(monitors))
> for (i in 1:length(monitors)){
+ dataframes[[i]] <- read.csv(monitors[[i]], sep = ",")
+ }
>它工作并将其保存为具有65.5MB重量的数据帧列表,问题是当我将其作为函数传递时。可能是因为词法作用域?
这个问题的解决方案是这个问题的最有价值的答案:How to make object created within function usable outside
https://stackoverflow.com/questions/47497727
复制相似问题