将pdf文件中的数据导入R
importing data from a pdf file into R
给定 page 4 of this PDF file,我想知道是否有办法(使用任何 R 库)将名为 SCALE SCORE
和 FREQ.
的 2 列作为 [=12] 导入 R =] 或其他 R 友好格式?
一次执行此操作的最快方法是 select 数据列(但不是列标题)。在 pdf 的第 4 页上,从左上角的 1038 到右下角的 100.00。将 selection 复制到剪贴板:
dta <- read.table(pipe("pbpaste")) # For Mac Os
# For Windows use dta <- read.table("clipboard")
dta <- dta[, 1:2] # Discard the last three columns
colnames(dta) <- c("ScaleScore", "Freq")
head(dta)
# ScaleScore Freq
# 1 1038 142
# 2 1171 15
# 3 1250 78
# 4 1299 269
# 5 1335 840
# 6 1364 1690
如果您想重复执行此操作,则必须安装软件包 pdftools
。
我写了一个包可以帮助从 pdf 中提取文本。它是用 C++ 从头开始编写的,速度相当快(通常比 pdftools 快一点)。目前您仍然需要将文本 w运行gle 转换为 table - 就像在 pdftools 中一样。在您的情况下,它会像这样工作:
library(dplyr)
library(PDFR)
df <- pdfpage("C:/users/Administrator/Documents/sales.pdf", 4)
df <- df[df$left > 440,] %>%
group_by(top) %>%
arrange(left, by_group = TRUE) %>%
summarize(text = paste(text, collapse = ",")) %>%
arrange(-top) %>%
filter(seq(nrow(.)) > 4) %>%
`[[`(2) %>%
read.csv(text = ., header = FALSE,
col.names = c("freq", "cum_freq", "perc", "cum_perc"))
这给你:
#> freq cum_freq perc cum_perc
#> 1 142 142 0.04 0.04
#> 2 15 157 0.00 0.04
#> 3 78 235 0.02 0.06
#> 4 269 504 0.07 0.13
#> 5 840 1344 0.21 0.34
#> 6 1690 3034 0.42 0.76
#> 7 3254 6288 0.81 1.57
#> 8 5413 11701 1.35 2.92
#> 9 7659 19360 1.91 4.83
#> 10 9696 29056 2.42 7.24
#> 11 11529 40585 2.87 10.12
#> 12 13145 53730 3.28 13.39
#> 13 13830 67560 3.45 16.84
#> 14 14844 82404 3.70 20.54
#> 15 15153 97557 3.78 24.32
#> 16 15120 112677 3.77 28.09
#> 17 15347 128024 3.83 31.92
#> 18 15525 143549 3.87 35.79
#> 19 15710 159259 3.92 39.70
#> 20 15596 174855 3.89 43.59
#> 21 15529 190384 3.87 47.46
#> 22 15451 205835 3.85 51.31
#> 23 15259 221094 3.80 55.12
#> 24 15028 236122 3.75 58.86
#> 25 15147 251269 3.78 62.64
#> 26 14683 265952 3.66 66.30
#> 27 14469 280421 3.61 69.91
#> 28 14229 294650 3.55 73.45
#> 29 13523 308173 3.37 76.82
#> 30 13246 321419 3.30 80.13
#> 31 12987 334406 3.24 83.36
#> 32 12264 346670 3.06 86.42
#> 33 11964 358634 2.98 89.40
#> 34 10841 369475 2.70 92.11
#> 35 9958 379433 2.48 94.59
#> 36 8529 387962 2.13 96.72
#> 37 6729 394691 1.68 98.39
#> 38 4437 399128 1.11 99.50
#> 39 2010 401138 0.50 100.00
虽然这可能看起来有点复杂,但对于像您这样的 pdf 非常有用,其中 table 在每一页上都是相同的。如果你 运行 上面的代码在 lapply
循环中,它可以一次获得多个页面,这比剪切和粘贴要快得多。
要安装你需要开发工具:
install.packages("devtools")
devtools::install_github("AllanCameron/PDFR")
编辑
如果有安装问题,这里是pdftools中的等价物:
install.packages(pdftools)
df <- pdftools::pdf_data("https://tea.texas.gov/sites/default/files/Scale%20Score%20Distribution%20Graph%201_Grade%203%20to%208%20English-r2_tagged.pdf")[[4]]
df <- df[df$x > 440,] %>%
group_by(y) %>%
arrange(x, by_group = TRUE) %>%
summarize(text = paste(text, collapse = ",")) %>%
arrange(y) %>%
`[[`(2) %>%
`[`(3:41) %>%
read.csv(text = ., header = FALSE,
col.names = c("freq", "cum_freq", "perc", "cum_perc"))
给定 page 4 of this PDF file,我想知道是否有办法(使用任何 R 库)将名为 SCALE SCORE
和 FREQ.
的 2 列作为 [=12] 导入 R =] 或其他 R 友好格式?
一次执行此操作的最快方法是 select 数据列(但不是列标题)。在 pdf 的第 4 页上,从左上角的 1038 到右下角的 100.00。将 selection 复制到剪贴板:
dta <- read.table(pipe("pbpaste")) # For Mac Os
# For Windows use dta <- read.table("clipboard")
dta <- dta[, 1:2] # Discard the last three columns
colnames(dta) <- c("ScaleScore", "Freq")
head(dta)
# ScaleScore Freq
# 1 1038 142
# 2 1171 15
# 3 1250 78
# 4 1299 269
# 5 1335 840
# 6 1364 1690
如果您想重复执行此操作,则必须安装软件包 pdftools
。
我写了一个包可以帮助从 pdf 中提取文本。它是用 C++ 从头开始编写的,速度相当快(通常比 pdftools 快一点)。目前您仍然需要将文本 w运行gle 转换为 table - 就像在 pdftools 中一样。在您的情况下,它会像这样工作:
library(dplyr)
library(PDFR)
df <- pdfpage("C:/users/Administrator/Documents/sales.pdf", 4)
df <- df[df$left > 440,] %>%
group_by(top) %>%
arrange(left, by_group = TRUE) %>%
summarize(text = paste(text, collapse = ",")) %>%
arrange(-top) %>%
filter(seq(nrow(.)) > 4) %>%
`[[`(2) %>%
read.csv(text = ., header = FALSE,
col.names = c("freq", "cum_freq", "perc", "cum_perc"))
这给你:
#> freq cum_freq perc cum_perc
#> 1 142 142 0.04 0.04
#> 2 15 157 0.00 0.04
#> 3 78 235 0.02 0.06
#> 4 269 504 0.07 0.13
#> 5 840 1344 0.21 0.34
#> 6 1690 3034 0.42 0.76
#> 7 3254 6288 0.81 1.57
#> 8 5413 11701 1.35 2.92
#> 9 7659 19360 1.91 4.83
#> 10 9696 29056 2.42 7.24
#> 11 11529 40585 2.87 10.12
#> 12 13145 53730 3.28 13.39
#> 13 13830 67560 3.45 16.84
#> 14 14844 82404 3.70 20.54
#> 15 15153 97557 3.78 24.32
#> 16 15120 112677 3.77 28.09
#> 17 15347 128024 3.83 31.92
#> 18 15525 143549 3.87 35.79
#> 19 15710 159259 3.92 39.70
#> 20 15596 174855 3.89 43.59
#> 21 15529 190384 3.87 47.46
#> 22 15451 205835 3.85 51.31
#> 23 15259 221094 3.80 55.12
#> 24 15028 236122 3.75 58.86
#> 25 15147 251269 3.78 62.64
#> 26 14683 265952 3.66 66.30
#> 27 14469 280421 3.61 69.91
#> 28 14229 294650 3.55 73.45
#> 29 13523 308173 3.37 76.82
#> 30 13246 321419 3.30 80.13
#> 31 12987 334406 3.24 83.36
#> 32 12264 346670 3.06 86.42
#> 33 11964 358634 2.98 89.40
#> 34 10841 369475 2.70 92.11
#> 35 9958 379433 2.48 94.59
#> 36 8529 387962 2.13 96.72
#> 37 6729 394691 1.68 98.39
#> 38 4437 399128 1.11 99.50
#> 39 2010 401138 0.50 100.00
虽然这可能看起来有点复杂,但对于像您这样的 pdf 非常有用,其中 table 在每一页上都是相同的。如果你 运行 上面的代码在 lapply
循环中,它可以一次获得多个页面,这比剪切和粘贴要快得多。
要安装你需要开发工具:
install.packages("devtools")
devtools::install_github("AllanCameron/PDFR")
编辑
如果有安装问题,这里是pdftools中的等价物:
install.packages(pdftools)
df <- pdftools::pdf_data("https://tea.texas.gov/sites/default/files/Scale%20Score%20Distribution%20Graph%201_Grade%203%20to%208%20English-r2_tagged.pdf")[[4]]
df <- df[df$x > 440,] %>%
group_by(y) %>%
arrange(x, by_group = TRUE) %>%
summarize(text = paste(text, collapse = ",")) %>%
arrange(y) %>%
`[[`(2) %>%
`[`(3:41) %>%
read.csv(text = ., header = FALSE,
col.names = c("freq", "cum_freq", "perc", "cum_perc"))