2016-08-05 22 views
1

Hier sind die DatenR Extrahieren von Daten aus PDF-

http://drdpat.bih.nic.in/Downloads/Rice-Varieties-1996-2012.pdf

Es ist ein pdf ist. Wenn Sie das PDF öffnen, werden Sie auf Seite 2 eine Tabelle finden, die ich extrahieren und in einem Datenrahmen speichern muss. Ich folgte diesem Link mit diesem Danach

https://ropensci.org/blog/2016/03/01/pdftools-and-jeroen

zu tun, egal was ich es nicht versucht, es in ein Tabellenformat zu konvertieren. Ich versuchte dies:

dat1 <- matrix(dat, byrow = TRUE,nrow = 12, ncol = 8) # it didn't work 

Versucht, die Scan-Funktion

dat.s <- scan(dat, what = "character", sep = " ", skip = 2) # no use 

Kann mir jemand helfen, mit diesem zu benutzen? Ich bin auch nur in dieser R

Dank

+0

Haben Sie es mit dem Tabulizer-Paket versucht? https://github.com/leeper/tabolizer –

+0

Nein, ich habe das nicht versucht. Ich werde nachsehen. Eigentlich weiß ich nicht, wie man Code aus GitHub kopiert und in R ausführt. Es gibt so viele Dinge in dem Link, den du angegeben hast. Vielen Dank – user53020

Antwort

0

Die Struktur der Tabellen in der PDF zu erreichen suchen, ist ein bisschen durcheinander: einige Spalten einander überlappen und die tabulizer Algorithmus funktioniert sie nicht richtig extrahieren.

Ich konnte nur die ersten 6 Spalten von Seite 2 extrahieren; die letzten 2 Spalten (hervorstechende Merkmale, "Empfohlen für den Anbau") bleiben problematisch ...

library(tabulizer) 
library(dplyr) 

out1 <- extract_tables("Rice-Varieties-1996-2012.pdf", pages=2)[[1]] 

## With a moderate amount of hacking, 
## the following columns are correctly extracted: 
## 1. Sl. No. 
## 4. Year of Notification 
## 5. Duration (in days) 
## 6. Eco-System 

sel <- gsub(" ","",out1[ ,c(1,4,5,6)]) 

## To extract Parentage column, you can use the `area` parameter: 
## I figured out the values by trial and error 
out2 <- extract_tables("Rice-Varieties-1996-2012.pdf", guess=FALSE, 
         pages=2, 
         area=list(c(80,120,2000,420)))[[1]] 
sel <- cbind(sel,out2[1:nrow(sel),1]) 

## The header is contained in the first 3 rows of `sel` 
## which can be aggregated by `paste0` 
print(sel) 
head <- aggregate(sel[1:3, ], by=list(rep(1,3)), paste0, collapse="") %>% 
    select(-Group.1) 

## The body is a bit harder, because each record might be split across 
## a variable number of rows, depending on the entries. 
## I have used non-empty records for column 1 (Sl.No.) 
## to identify the breakpoints where to split sel into row blocks 
## pertaining to the same record. 
body <- sel[-(1:3), ] 
brks <- body[ ,1]!="" 
ibrk <- c((1:nrow(body))[brks], nrow(body)+1) 
ll <- unlist(sapply(1:(length(ibrk)-1), function(k) rep(ibrk[k],ibrk[k+1]-ibrk[k]))) 

stopifnot(length(ll)==nrow(body)) 

body <- data.frame(body, stringsAsFactors=FALSE) 
colnames(body) <- head 

tab <- aggregate(body, by=list(ll), paste0, collapse="") %>% 
    select(-Group.1) 

print(tab) 

## Using the same trick as above with brks and ibrk, 
## one is able to extract column "Name of variety" 
## (again, I found the values of area by trial and error). 
out3 <- extract_tables("Rice-Varieties-1996-2012.pdf", guess=FALSE, 
         pages=2, 
         area=list(c(80,20,2000,130)))[[1]] 
sel3 <- gsub(" ","",out3) 
head3 <- aggregate(sel3[1:2, ], by=list(rep(1,2)), paste0, collapse="") %>% 
    select(-Group.1) 
body3 <- sel3[-(1:2), ] 
brks3 <- body3[ ,1]!="" 
ibrk3 <- c((1:nrow(body3))[brks3], nrow(body3)+1) 
ll3 <- unlist(sapply(1:(length(ibrk3)-1), function(k) rep(ibrk3[k],ibrk3[k+1]-ibrk3[k]))) 

stopifnot(length(ll3)==nrow(body3)) 
body3 <- data.frame(body3, stringsAsFactors=FALSE) 
colnames(body3) <- head3 

tab3 <- aggregate(body3, by=list(ll3), paste0, collapse="") %>% 
    select(-Group.1) 

print(tab3) 

## I have not managed to find a value of `area` which correctly splits 
## the last two columns *and* allows to identify the rows in each record... 

tab <- tab %>% left_join(tab3)