加载文件并将函数应用到代码中的特定组件
我有许多想要加载的外部文件,可以根据列表名称和文件名称将其应用于我的列表l1
。
例如: 如果我有 3 个 .csv 文件“2008.csv”、“2009.csv”、“2010.csv”,并且这些 .csv 文件中的数据将作为数据框加载。我想将这些数据帧加载到列表中,如下所示:
“2008.csv”到 l1
中的第一个元素(即列表中的 2008)
“2009.csv”到第二个元素将 l1
(列表中的 2009)中的
“2010.csv”插入 l1
中的第三个元素(列表中的 2010)。
在这种情况下,.csv 文件针对列表的特定元素(即“2009.csv”不会进入 2008 年和 2010 年。
有没有办法告诉 R 查看文件的名称并加载它们到列表的特定组成部分?
l1 <- list(NULL, NULL, NULL)
names(l1) <- c("2008", "2009", "2010")
I have a number of external files that I would like to load in that can be applied to my list l1
based on the name of the list and the names of the files.
For example:
If I have 3 .csv files "2008.csv", "2009.csv", "2010.csv", and the data from these .csv files would be loaded in as a data frame. I would like to load in these data frames into the list as follows:
"2008.csv" into the first element in l1
(i.e., 2008 in the list)
"2009.csv" into the second element in l1
(2009 in the list)
"2010.csv" into the third element in l1
(2010 in the list).
In this case, the .csv files are targeting specific elements of a list (i.e., "2009.csv" is not going into either 2008 and 2010.
Is there a way to tell R to look at the names of the files and load them into specific components of a list?
l1 <- list(NULL, NULL, NULL)
names(l1) <- c("2008", "2009", "2010")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个基于用户提供的名称列表使用
lapply
的简单解决方案:(这没有 akrun 在评论中的建议复杂,它搜索文件并提取文件名。)
A simple solution using
lapply
based on a user-supplied list of names:(This is less sophisticated than akrun's suggestion in the comment, which searches over files and extracts the filename.)
您可以按如下方式执行此操作:
You can do this as follows :