将多个CSV文件读入单独的数据框 [英] Read multiple CSV files into separate data frames

查看:217
本文介绍了将多个CSV文件读入单独的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在目录 C中有文件file1.csv file2.csv ,...和 file100.csv :\R\Data ,我们要将它们全部读入单独的数据框架(例如 file1 file2 ,...和 file100 )。

Suppose we have files file1.csv, file2.csv, ... , and file100.csv in directory C:\R\Data and we want to read them all into separate data frames (e.g. file1, file2, ... , and file100).

这样做的原因是,尽管名称相似,但它们具有不同的文件结构,因此在列表中使用它们并不有用。

The reason for this is that, despite having similar names they have different file structures, so it is not that useful to have them in a list.

我可以使用 lapply 但返回一个包含100个数据帧的单个列表。我想在全球环境中使用这些数据框架。

I could use lapply but that returns a single list containing 100 data frames. Instead I want these data frames in the Global Environment.

如何将多个文件直接读入全局环境?或者,如何解压缩数据框列表中的内容?

How do I read multiple files directly into the global environment? Or, alternatively, How do I unpack the contents of a list of data frames into it?

推荐答案

快速草稿,未经测试:


  1. 使用 list.files() aka dir()可动态生成文件列表。

  1. Use list.files() aka dir() to dynamically generate your list of files.

这会返回一个向量,只需沿向量在中循环

This returns a vector, just run along the vector in a for loop.

读取第i个文件,然后使用 assign()将内容放入新变量file_i


Read the i-th file, then use assign() to place the content into a new variable file_i

这对你来说应该可以。

这篇关于将多个CSV文件读入单独的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆