简体   繁体   English

从Rproj中的列表创建数据框

[英]Create dataframe from list in Rproj

I have an issue that really bugs me: I've tried to convert to Rproj lately, because I would like to make my data and scripts available at some point. 我有一个确实困扰我的问题:我最近尝试转换为Rproj,因为我想在某个时候使我的数据和脚本可用。 But with one of them, I get an error that, I think, should not occur. 但是,对于其中之一,我认为应该不会发生错误。 Here is the tiny code that gives me so much trouble, the R.proj being available at: https://github.com/fredlm/mockup . 这是给我带来很多麻烦的小代码,R.proj可从以下网址获得: https : //github.com/fredlm/mockup

    library(readxl)
list <- list.files(path = "data", pattern = "file.*.xls") #List excel files

#Aggregate all excel files
df <- lapply(list, read_excel)
for (i in 1:length(df)){
  df[[i]] <- cbind(df[[i]], list[i])
}
df <- do.call("rbind", df)

It gives me the following error right after "df <- lapply(list, read_excel)": 它在“ df <-lapply(list,read_excel)”之后给出以下错误:

Error in read_fun(path = path, sheet = sheet, limits = limits, shim = shim, : path[1]="file_1.xls": No such file or directory read_fun中的错误(路径=路径,工作表=工作表,限制=限制,垫片=垫片,:路径[1] =“ file_1.xls”:没有此类文件或目录

Do you know why? 你知道为什么吗? When I do it old school, ie using 'setwd' before creating 'list', everything works just fine. 当我做旧学校时,即在创建“列表”之前使用“ setwd”,一切都很好。 So it looks like lapply does not know where to look for the file when used in a Rproj, which seems very odd... 所以看起来Lapply在Rproj中使用时不知道在哪里寻找文件,这似乎很奇怪。

What did I miss? 我错过了什么?

Thanks :) 谢谢 :)

Thanks to a non-stackoverflower, a solution was found. 多亏了非stackoverflower,找到了解决方案。 It's silly, but 'list' was missing a directory, so lapply couldn't aggregate the data. 这很傻,但是'list'缺少目录,因此lapply无法聚合数据。 The following works just fine: 以下工作正常:

list <- paste("data/", list.files(path = "data", pattern = pattern = "file.*.xls"), sep = "") #List excel files

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

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