如何将文档中的某些单词提取到 R 中的数据框中?

How do I extract certain words in my document into a dataframe in R?

编辑:可重现的例子(我希望我做对了):

我继续使用 as.character(docs[1]) 创建一个字符串,以实现可重复性:

"list(list(content = c(\"Name: Birthdate (MM/DD): Print Date: Student ID: Institution ID: Page:\", \"\", \"MyName MyBirthday 06/16/2015 N1111111 002785 1 of 1\", \"\", \"A string I don't want\", \"\", \"More stuff I don't want\", \"Don't want\", \"\", \"Names of Classes\", \n\"\", \"Class numbers and sections that I don't want\", \"\", \"Current Cumulative\", \"\", \"AHRS (don't want)\", \"12.0 12.0 (no)\", \"\", \"EHRS (WANT THIS)\", \"12.0 12.0\", \"\", \"QHRS (no)\", \"12.0 12.0\", \"\", \"QPTS (no) \", \" (no) 45.900 45.900\", \"\", \"GPA\", \"3.825 3.825\", \"\", \"Spring 2015\", "etc", \"\", \"End of Graduate Record\", \"\", \"\f\"), meta = list(author = NULL, datetimestamp = NULL, description = NULL, heading = NULL, id = \"Unofficial June 2015 copy 2.pdf\", language = \"en\", origin = NULL)))"

我想要摆脱这个混乱的是身份证号码(在这个例子中是 N1111111),学期(2014 年秋季和 Spring 2015 年),EHRS 后面的数字(12.0 12.0,每个在它的自己的列),以及 GPA 后面的数字(3.825 3.825,每个都在自己的列中)。


我有学术成绩单的文本数据需要放入数据框进行分析。我已将成绩单 pdf 转换为文本,但现在我需要数据框中的某些信息。具体来说,我需要以下列中的数据:

学生 ID,秋季 1 当前小时数,秋季 1 累计小时数,秋季 1 当前 GPA,Spring 1 当前小时数,Spring 1 累计小时数,Spring 1 当前 GPA, Spring 1 累计 GPA,夏季 1 当前学时,夏季 1 累计学时,夏季 1 当前 GPA,夏季 1 累计 GPA

等,学生每学期留在大学。

小时数来自EHRS,没有列出暑期课程的情况被视为0当前小时数,0当前gpa,累计小时数和gpa与前一个spring相同它。

到目前为止,我已经使用 tm 库将 pdf 转换为文本,并具有以下示例抄本:

docs <- Corpus(DirSource(cname), readerControl=list(reader=readPDF()))

inspect(docs[1])

学生姓名 MM/YY 06/16/2015 N11111111 002785 1 of 1

大学名称毕业记录开始

2014 年秋季学校名称 理学硕士专业:专业

Class 的名称 1 Class 的名称 2 Class 的名称 3 Class 的名称 4

课程+部分 3.0 B+ 课程+部分 3.0 A 课程+部分 3.0 A 课程+部分 3.0 A

当前累计

AHRS 12.0 12.0

电子病历 12.0 12.0

QHRS 12.0 12.0

QPTS 45.900 45.900

平均成绩 3.825 3.825

Spring 2015

学校名称 理学硕士专业:专业

Class 的名称 1 Class 的名称 2 Class 的名称 3

COURSE+SECTION 2.0 A COURSE+SECTION 2.0 A COURSE+SECTION 2.0 A-

Class 4 课程+部分 2.0 A 的名称

Class5 的名字

课程+部分 2.0 A-

Class 6 课程+部分 4.0 A 的名称

Class7 的名字

课程+部分 3.0 B+

Class8 的名字

课程+部分

3.0 安

当前累计

AHRS 20.0 32.0

电子病历 20.0 32.0

QHRS 20.0 32.0

QPTS 76.700 122.600

平均成绩 3.835 3.831

毕业生记录结束

这是我在文档看起来相似时使用的策略。如果文件完全相同。您可以跳过大部分 grep() 并使用直接引用(即 txt[1])到您要解析的信息的位置。

提取策略:

  • 使用grep 来识别目标行。使用锚 ^$ 效果很好。
  • 确定目标行后,使用 strsplit 拆分为所需的元素。重复最后一步。
  • 尽可能使用直接引用 (txt[1]) 或正则表达式 (txt[grep("GPA",txt)])。
  • 以您喜欢的任何方式解析和重新格式化。

读行

txt <- readLines(con=textConnection(
'Student Name MM/YY 06/16/2015 N11111111 002785 1 of 1

Name of University Beginning of Graduate Record

Fall 2014 Name of School Master of Science Major: Major

Name of Class 1 Name of Class 2 Name of Class 3 Name of Class 4

COURSE+SECTION 3.0 B+ COURSE+SECTION 3.0 A COURSE+SECTION 3.0 A COURSE+SECTION 3.0 A

Current Cumulative

AHRS 12.0 12.0

EHRS 12.0 12.0

QHRS 12.0 12.0

QPTS 45.900 45.900

GPA 3.825 3.825

Spring 2015

Name of School Master of Science Major: Major

Name of Class 1 Name of Class 2 Name of Class 3

COURSE+SECTION 2.0 A COURSE+SECTION 2.0 A COURSE+SECTION 2.0 A-

Name of Class 4 COURSE+SECTION 2.0 A

Name of Class 5

COURSE+SECTION 2.0 A-

Name of Class 6 COURSE+SECTION 4.0 A

Name of Class 7

COURSE+SECTION 3.0 B+

Name of Class 8

COURSE+SECTION

3.0 A

Current Cumulative

AHRS 20.0 32.0

EHRS 20.0 32.0

QHRS 20.0 32.0

QPTS 76.700 122.600

GPA 3.835 3.831

End of Graduate Record'))

清洁

# trim of 
trim <- function (x) gsub("^\s+|\s+$", "", x)
txt <- trim(txt)
# Drop empties
txt <- txt[txt != ""]

搜索解析:身份证号码

id <- strsplit(txt[1], " ")
id <- id[grep("^[N][0-9]",id)] # Starts with N followed by 0-9

搜索和解析:GPA

gpa <- txt[grep("GPA",txt)]
gpa <- strsplit(gpa, " ")
gpa <- matrix(
  as.numeric(
    t(
      as.data.frame(gpa)
      )[1:2, 2:3]
    ),ncol = 2)

...等等。