[英]how to clean up data in R
enter image description here I'm helping out my professor by automating one of his work. enter image description here我正在通过自动化他的一项工作来帮助我的教授。 He has a word document that contains information of students in tabular form and there are many tables in each document (sometimes in the hundreds).他有一个 word 文档,其中包含表格形式的学生信息,每个文档中有很多表格(有时有数百个)。 I've created the function below in R to extract data from Word我在下面的 R 中创建了 function 来从 Word 中提取数据
library(xml2)
get_tbls <- function(word_doc) {
tmpd <- tempdir()
tmpf <- tempfile(tmpdir=tmpd, fileext=".zip")
file.copy(word_doc, tmpf)
unzip(tmpf, exdir=sprintf("%s/docdata", tmpd))
doc <- read_xml(sprintf("%s/docdata/word/document.xml", tmpd))
unlink(tmpf)
unlink(sprintf("%s/docdata", tmpd), recursive=TRUE)
ns <- xml_ns(doc)
tbls <- xml_find_all(doc, ".//w:tbl", ns=ns)
lapply(tbls, function(tbl) {
cells <- xml_find_all(tbl, "./w:tr/w:tc", ns=ns)
rows <- xml_find_all(tbl, "./w:tr", ns=ns)
dat <- data.frame(matrix(xml_text(cells),
ncol=(length(cells)/length(rows)),
byrow=TRUE),
stringsAsFactors=FALSE)
colnames(dat) <- dat[1,]
dat <- dat[-1,]
rownames(dat) <- NULL
dat
})
}
this function extracts data from word document and after that, I've used writexl package to export the data to excel but it is also displaying stuff i don't need.这个 function 从 word 文档中提取数据,之后,我使用 writexl package 将数据导出到 excel,但它也显示我不需要的东西。
It's unclear what you need but I would probably use dplyr to clean the data frame.目前尚不清楚您需要什么,但我可能会使用 dplyr 来清理数据框。
Using your data frame dat:使用您的数据框 dat:
dat <- dat %>%
select(c(text, level))%>%
filter(text!="")%>%
filter(level!="")
or或者
dat <- dat %>%
select(-c(doc_index, content_type, is_header))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.