创建整洁的文本
Creating Tidy Text
我正在使用 R 进行文本分析。我使用 'readtext' 函数从 pdf 中提取文本。但是,如您所想,它非常混乱。我使用 'gsub' 来替换用于不同目的的文本。总体目标是使用一种类型的分隔符 '%%%%%' 将记录拆分为行,并使用另一种分隔符 '@' 拆分为列。我完成了第一个,但不知道如何完成后者。在数据框中找到的数据示例如下:
895 "The ambulatory case-mix development project\n@Published:: June 6, 1994@Authors: Baker A, Honigfeld S, Lieberman R, Tucker AM, Weiner JP@Country: United States @Journal:Project final report. Baltimore, MD, USA: Johns Hopkins University and Aetna Health Plans. Johns Hopkins\nUniversity and Aetna Health Plans, USA As the US […"
896 "Ambulatory Care Groups: an evaluation for military health care use@Published:: June 6, 1994@Authors: Bolling DR, Georgoulakis JM, Guillen AC@Country: United States @Journal:Fort Sam Houston, TX, USA: United States Army Center for Healthcare Education and Studies, publication #HR 94-\n004. United States Army Center for Healthcare Education and […]@URL: http://oai.dtic.mil/oai/oai?verb=getRecord&metadataPrefix=html&identifier=ADA27804"
我想获取此数据并将@Published、@Authors、@Journal、@URL 拆分为列 -- c("Published"、"Authors"、"Journal", "URL").
有什么建议吗?
提前致谢!
这似乎工作正常:
dfr <- data.frame(TEXT=c("The ambulatory case-mix development project\n@Published:: June 6, 1994@Authors: Baker A, Honigfeld S, Lieberman R, Tucker AM, Weiner JP@Country: United States @Journal:Project final report. Baltimore, MD, USA: Johns Hopkins University and Aetna Health Plans. Johns Hopkins\nUniversity and Aetna Health Plans, USA As the US […",
"Ambulatory Care Groups: an evaluation for military health care use@Published:: June 6, 1994@Authors: Bolling DR, Georgoulakis JM, Guillen AC@Country: United States @Journal:Fort Sam Houston, TX, USA: United States Army Center for Healthcare Education and Studies, publication #HR 94-\n004. United States Army Center for Healthcare Education and […]@URL: http://oai.dtic.mil/oai/oai?verb=getRecord&metadataPrefix=html&identifier=ADA27804"),
stringsAsFactors = FALSE)
library(magrittr)
do.call(rbind, strsplit(dfr$TEXT, "@Published::|@Authors:|@Country:|@Journal:")) %>%
as.data.frame %>%
setNames(nm = c("Preamble","Published","Authors","Country","Journal"))
基本上将文本拆分为四个字段之一(注意双 :: 发布后!),行绑定结果,转换为数据框,并给出一些名称。
我正在使用 R 进行文本分析。我使用 'readtext' 函数从 pdf 中提取文本。但是,如您所想,它非常混乱。我使用 'gsub' 来替换用于不同目的的文本。总体目标是使用一种类型的分隔符 '%%%%%' 将记录拆分为行,并使用另一种分隔符 '@' 拆分为列。我完成了第一个,但不知道如何完成后者。在数据框中找到的数据示例如下:
895 "The ambulatory case-mix development project\n@Published:: June 6, 1994@Authors: Baker A, Honigfeld S, Lieberman R, Tucker AM, Weiner JP@Country: United States @Journal:Project final report. Baltimore, MD, USA: Johns Hopkins University and Aetna Health Plans. Johns Hopkins\nUniversity and Aetna Health Plans, USA As the US […"
896 "Ambulatory Care Groups: an evaluation for military health care use@Published:: June 6, 1994@Authors: Bolling DR, Georgoulakis JM, Guillen AC@Country: United States @Journal:Fort Sam Houston, TX, USA: United States Army Center for Healthcare Education and Studies, publication #HR 94-\n004. United States Army Center for Healthcare Education and […]@URL: http://oai.dtic.mil/oai/oai?verb=getRecord&metadataPrefix=html&identifier=ADA27804"
我想获取此数据并将@Published、@Authors、@Journal、@URL 拆分为列 -- c("Published"、"Authors"、"Journal", "URL").
有什么建议吗?
提前致谢!
这似乎工作正常:
dfr <- data.frame(TEXT=c("The ambulatory case-mix development project\n@Published:: June 6, 1994@Authors: Baker A, Honigfeld S, Lieberman R, Tucker AM, Weiner JP@Country: United States @Journal:Project final report. Baltimore, MD, USA: Johns Hopkins University and Aetna Health Plans. Johns Hopkins\nUniversity and Aetna Health Plans, USA As the US […",
"Ambulatory Care Groups: an evaluation for military health care use@Published:: June 6, 1994@Authors: Bolling DR, Georgoulakis JM, Guillen AC@Country: United States @Journal:Fort Sam Houston, TX, USA: United States Army Center for Healthcare Education and Studies, publication #HR 94-\n004. United States Army Center for Healthcare Education and […]@URL: http://oai.dtic.mil/oai/oai?verb=getRecord&metadataPrefix=html&identifier=ADA27804"),
stringsAsFactors = FALSE)
library(magrittr)
do.call(rbind, strsplit(dfr$TEXT, "@Published::|@Authors:|@Country:|@Journal:")) %>%
as.data.frame %>%
setNames(nm = c("Preamble","Published","Authors","Country","Journal"))
基本上将文本拆分为四个字段之一(注意双 :: 发布后!),行绑定结果,转换为数据框,并给出一些名称。