本文介绍了在R中导入多个文本文件,并从预定列表中分配它们的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用R而我无法执行以下任务:我在工作目录中的单独纯文本文件中有大约130个语言示例。我想做的是使用扫描导入它们并保留它们的文件名。
具体来说,我想做的是使用以下内容:

I just started using R and I am having trouble performing the following task: I have approximately 130 language samples in separate plain text files sitting in my working directory. What I would like to do is import them using scan and retain their file names.Specifically, what I would like to do is using something like:

Patient01.txt <-scan("./Patient01.txt", what = "character")
Patient02.txt <-scan("./Patient02.txt", what = "character")
...
Patient130.txt <-scan("./Patient130.txt", what = "character")

有没有办法使用* apply这样的命令来自动化这个过程?

Is there a way to use a command such as *apply to automate the process?

推荐答案

这是一种自动化的方法进程

Here is one way to automate the process

# read txt files with names of the form Patient*.txt
txt_files = list.files(pattern = 'Patient*.txt');

# read txt files into a list (assuming separator is a comma)
data_list = lapply(txt_files, read.table, sep = ",")

如果你知道它是什么,你可以更改分隔符。将数据保存为数据帧列表很方便,因为它更容易投入向量化操作或稍后循环。

You can change the separator if you know what it is. It is convenient to keep the data as a list of data frames since it is easier to throw into a vectorized operation or loops later.

这篇关于在R中导入多个文本文件,并从预定列表中分配它们的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 11:00