【发布时间】:2018-03-15 09:16:57
【问题描述】:
我有一个带有表格的 PDF 文件。我正在使用pdftools::pdf_text 函数来提取文本,我得到了一个带有几行代表表格的向量。
我的问题是,只有空格,有些单元格有多行。我正在寻找一种将其放入数据框中的策略。我用data.table(text='') 玩了一下,但这是在抱怨每行的元素数量不匹配。
Name Separator Description
Protein IDs Identifier(s) of protein(s) contained in the protein group. They
are sorted by number of identified peptides in descending
order.
Majority protein IDs These are the IDs of those proteins that have at least half of
the peptides that the leading protein has.
Peptide counts (all) Number of peptides associated with each protein in protein
group, occuring in the order as the protein IDs occur in the
'Protein IDs' column. Here distinct peptide sequences are
counted. Modified forms or different charges are counted as
one peptide.
这是数据
my_lines <- c(" Name Separator Description",
" Protein IDs Identifier(s) of protein(s) contained in the protein group. They",
" are sorted by number of identified peptides in descending",
" order.", " Majority protein IDs These are the IDs of those proteins that have at least half of",
" the peptides that the leading protein has.",
" Peptide counts (all) Number of peptides associated with each protein in protein",
" group, occuring in the order as the protein IDs occur in the",
" 'Protein IDs' column. Here distinct peptide sequences are",
" counted. Modified forms or different charges are counted as",
" one peptide."
)
编辑:
我预期的输出将是这样的数据框:
Name Separator
1 Protein IDs
2 Majority protein IDs
3 Peptide counts (all)
Description
1 Identifier(s) of protein(s) contained in the protein group. They are sorted by number of identified peptides in descending order.
2 These are the IDs of those proteins that have at least half of the peptides that the leading protein has.
3 Number of peptides associated with each protein in protein group, occuring in the order as the protein IDs occur in the 'Protein IDs' column. Here distinct peptide sequences are counted. Modified forms or different charges are counted as one peptide.
EDIT2:
所以玩readr::read_fwf()我用下面的代码更接近了(我没有太注意列的起点和终点,只是测试了它)。
writeLines(my_lines, 'test.txt')
readr::read_fwf('test.txt',
fwf_positions(c(1, 30, 45), c(29, 42, 300),
c("Name", "Separator", "Description")),
skip=1)
这里的问题是,Name 列中的空行我得到了 Gas。由于Description 列有多行,因此它还需要Name 的值,但没有。
【问题讨论】:
-
你的预期输出是什么?
-
添加了预期的输出。我目前正在尝试使用
readr::read_fwf(),这可能会有所帮助,但在计算单元格宽度时遇到了困难。理想的做法是跳过Separator,但之后可以将其删除。
标签: r read.table readr