繁体   English   中英

从R中的文件夹读取多个以空格分隔的文本文件

[英]Reading multiple space-delimited text files from a folder in R

我在一个文件夹中有100个以space-delimited文本文件。 每个文本文件中都有一段文本。 我希望提取数据框中的数据,其中第column 1列为File IDColumn 2列为相应的文本段落。

到目前为止,这是我尝试过的操作,但是未能以所需的格式提取文本段落。

lf <- list.files(path = "", pattern = "'*.txt", full.names = TRUE, recursive = TRUE, include.dirs = TRUE)
data <- lapply(lf, read.table, sep="", header=FALSE)

示例文本文件如下所示:

“是的,我不断打来电话,我一直在问是否Dvr正在达成促销协议,因为我在漏斗和延误方面遇到了一些问题,而今天。 Dvr的。”

我得到的输出是一个列表:

[[1]]
     V1  V2  V3       V4    V5    V6 V7 V8   V9 V10 V11       V12 V13          V14 V15 V16     V17
1 Yeah, and and repeated phone calls is  I call  in  on something   I continuously ask  if there's
  V18         V19  V20   V21 V22   V23     V24  V25 V26  V27      V28  V29 V30    V31 V32 V33
1   a promotional deal going  on Dvr's because I've had some problems with the hopper and the
     V34 V35    V36 V37 V38     V39  V40 V41        V42    V43    V44    V45 V46    V47 V48 V49
1 delays and today.   I get another bill  or exchanging hopper enjoys better for Dvr's.

我希望以数据框格式获取它,如下所示:

File ID         Text

file1.txt       Yeah, and and repeated phone calls...

关于我所缺少的内容有什么建议吗?

提前致谢。

尝试以下操作:(您不希望将空格用作分隔符,因为在段落中有很多分隔符):

dat <- setNames( lapply(lf, read.table, sep="|", header=FALSE), lf)

选择您怀疑不在文本中的分隔符。 恐怕sep=""是一个错误的选择,因为它被解释为read.table的默认值,即“ whitespace”。 每个文件的条目的“标题”应为文件名。

暂无
暂无

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

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