1
我試圖從pdftools
包中產生的字符向量中提取表格數據。輸出(修剪時)看起來是這樣的:使用pdftools從PDF中讀取表格
pdftext <- c(" Clostridium perfringens no./100ml 0 0 0 0 1409 0 0\n Colony count 22°C cfu/ml - 0 15.673 >300.000 52 0 0\n Colony count 37°C cfu/ml - 0 3.942 41 52 0 0")
lines <- strsplit(pdftext,"\n")
lines
[[1]]
[1] " Clostridium perfringens no./100ml 0 0 0 0 1409 0 0"
[2] " Colony count 22°C cfu/ml - 0 15.673 >300.000 52 0 0"
[3] " Colony count 37°C cfu/ml - 0 3.942 41 52 0 0"
我已經分裂,並使用這些連接成一個字符矩陣:
do.call(rbind,lapply(lines, function(x) {strsplit(x," [ ]+")})[[1]])
失敗的原因有包含列之間只有一個空格15.673 >300.000
。
可能有不止一個的問題在這裏:
- 我如何通過一個特徵向量來read.fwf?
- 你能提出一個更好的正則表達式嗎?分裂
15.673 >300.000
但不是Colony count 22°C
? - 是否有更簡單的方法拆分此表?我的代碼看起來有點笨拙。
謝謝。
not read but read.table(text = pdftext,row.names = NULL)'close close – rawr
or'read.csv(text = gsub('{2,} |(?=>)',' ',trimws(pdftext),perl = TRUE),row.names = NULL,stringsAsFactors = FALSE)' – rawr
@rawr'read.table'(和'read.fwf')給出這個錯誤「'file'must be a character字符串或連接「。我應該在問題1中詳細說明。除了第一行成爲列標題 – whanrott