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