Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用扫描处理多个文本文件_R_Text Files - Fatal编程技术网

R 使用扫描处理多个文本文件

R 使用扫描处理多个文本文件,r,text-files,R,Text Files,我有这个代码对我有用(它来自Jockers的文本分析,针对文学专业的学生使用R)。然而,我需要能够做到的是自动化:我需要为多达30个单独的文本文件执行“ProcessingSection”。我该怎么做?我是否可以拥有一个表或数据框,其中包含每次扫描(“*.txt”)时出现的30个“text.v” 非常感谢您的帮助 # Chapter 5 Start up code setwd("D:/work/cpd/R/Projects/5/") text.v <- scan("pupil-14.t

我有这个代码对我有用(它来自Jockers的文本分析,针对文学专业的学生使用R)。然而,我需要能够做到的是自动化:我需要为多达30个单独的文本文件执行“ProcessingSection”。我该怎么做?我是否可以拥有一个表或数据框,其中包含每次
扫描(“*.txt”)
时出现的30个“text.v”

非常感谢您的帮助

# Chapter 5 Start up code

setwd("D:/work/cpd/R/Projects/5/")

text.v <- scan("pupil-14.txt", what="character", sep="\n")
length(text.v)


#ProcessingSection
text.lower.v <- tolower(text.v)
mars.words.l <- strsplit(text.lower.v, "\\W")
mars.word.v <- unlist(mars.words.l)

#remove blanks
not.blanks.v <- which(mars.word.v!="")
not.blanks.v

#create a new vector to store the individual words
mars.word.v <- mars.word.v[not.blanks.v]
mars.word.v
#第5章启动代码
setwd(“D:/work/cpd/R/Projects/5/”)

text.v很难帮助你,因为你的例子不是

承认你对mars.word.v的结果感到满意, 您可以将这部分代码转换为接受单个参数的函数, 扫描结果

processing_section <- function(x){
  unlist(strsplit(tolower(x), "\\W"))
}

这就是你想要的吗?

谢谢,文森特。让函数返回包含text.v类型列表的表或数据框(对不起,新手)会更容易吗?(例如,扫描多个.txt文件后返回的向量列表,然后我使用该列表或向量数据帧上的下标访问每个“text.v”?取决于您想做什么,但不确定是否理解您的问题。Vincent,我真的很感激。我能读一些文本文件吗?学生故事-和aftrr每一个都是r通过扫描读取()每个文本文件都存储在一个向量中,包含文本文件内容的向量列表/向量表以向量表或向量列表的形式返回。你让它看起来很简单。谢谢。我希望开始攻读教育博士学位-我对英语语言使用感兴趣-口语和书面-当学生(13-14岁)使用多用户模拟程序学习和写科学。我代表他们感谢你们!
lf <- list.files(pattern=".txt")
lapply(lf, function(path) processing_section(scan(path, what="character", sep="\n")))