R-在数据框中显示数据

问题描述

使用以下代码,我使用pdftools从pdf文件中提取数据:

library(pdftools)
library(readr)

download.file("https://www.stoxx.com/document/Reports/SelectionList/2020/August/sl_sxebmp_202008.pdf","sl_sxebmp_202008.pdf",mode = "wb")
txt <- pdf_text("sl_sxebmp_202008.pdf")

txt <- read_lines(txt)

print(txt)

如何将这些数据显示为data.frame?

解决方法

我建议您使用一种tabulizer方法来使用您的文件。您可以使用extract_tables()将所有数据放入列表中,然后进行处理。列表中的第一个元素将包含变量名称,因此最好先处理此元素。接下来的代码:

library(tabulizer)
#Read
lst <- extract_tables(file = 'sl_sxebmp_202008.pdf') 
#Format
#Split elements as first element has variable names
d1 <- lst[[1]]
lst2 <- lst[2:length(lst)]
#Process
#Format first element
d1 <- as.data.frame(d1,stringsAsFactors = F)
names(d1) <- d1[1,]
d1 <- d1[2:dim(d1)[1],]
#Format list
lst2 <- lapply(lst2,function(x) {x <- as.data.frame(x,stringsAsFactors=F)})
#Bind all element in lst2
d2 <- do.call(rbind,lst2)
#Assign same names
names(d2) <- names(d1)
#Bind all
d3 <- rbind(d1,d2)

输出d3的某些行(1753行和11列):

          ISIN   Sedol     RIC Int.Key Company Name Country Currency Component FF Mcap (BEUR)
1 CH0038863350 7123870  NESN.S  461669       NESTLE      CH      CHF         Y          299.1
2 CH0012032048 7110388   ROG.S  474577 ROCHE HLDG P      CH      CHF         Y          206.4
3 CH0012005267 7103065  NOVN.S  477408     NOVARTIS      CH      CHF         Y          173.1
4 DE0007164600 4846288 SAPG.DE  476361          SAP      DE      EUR         Y          146.4
5 NL0010273215 B929F46 ASML.AS  546078    ASML HLDG      NL      EUR         Y          127.6
6 GB0009895292 0989529   AZN.L  098952  ASTRAZENECA      GB      GBP         Y          124.2
  Rank\r(FINAL) Rank\r(PREVIO\rUS)
1             1                  1
2             2                  2
3             3                  3
4             4                  5
5             5                  4
6             6                  6

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...