【问题标题】:creating a table from plain text with multi line entries in r从纯文本创建一个表,在 r 中具有多行条目
【发布时间】: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


【解决方案1】:

使用dplyr::summarisetidyr::fill 可以实现一个解决方案。

方法: 文本(i.e. Separator, Description) 在第一行x[1] 中的位置可以被视为在后续行中划分文本的指南。该规则适用于从pdf 中的表中提取的数据。使用这些位置将每一行分成 3 列并准备一个 data.frame。最后,应用合并/汇总技术来获得所需的结果。

df <- rbind.data.frame(cbind(substr(x, 1, (regexpr("Separator", x[1])[1]-1)), 
           substr(x,regexpr("Separator", x[1])[1], 47), 
           substr(x, (regexpr("Description", x[1])[1]-1), nchar(x))),
            stringsAsFactors = FALSE)

#Rename columns
names(df) <- trimws(df[1,])
#remove 1st row
df <- df[-1,]

library(tidyverse)
df %>% mutate(Name = ifelse(trimws(Name) == "", NA, trimws(Name))) %>%
     fill(Name) %>%
     group_by(Name) %>%
     summarise(Description = paste(Description, collapse=""))


# Name                 Description                                                                                                                              
# <chr>                <chr>                                                                                                                                    
# 1 Majority protein IDs These are the IDs of those proteins that have at least half ofthe peptides that the leading protein has.                                 
# 2 Peptide counts (all) Number of peptides associated with each protein in proteingroup, occuring in the order as the protein IDs occur in the'Protein IDs' colu~
# 3 Protein IDs          Identifier(s) of protein(s) contained in the protein group. Theyare sorted by number of identified peptides in descendingorder. 

数据

x <- 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."
)

【讨论】:

    【解决方案2】:

    这是一个循环遍历文本行的基本 R 选项:

    df <- data.frame(name=character(), text=character())
    col <- ""
    content <- ""
    for (row in 2:length(text)) {
        if (grepl("^\\s{1,10}[^[:space:]]", text[row])) {
            if (content != "") {
                df <- rbind(df, data.frame(col, content))
            }
            col <- gsub("^\\s*(.*?)(\\s{10,}).*", "\\1", text[row], perl=TRUE)
            content <- ""
            content <- gsub(".*\\s{10,}(.*)$", "\\1", text[row], perl=TRUE)
        } else {
            content <- paste(" ", content, gsub("^\\s+(.*)", "\\1", text[row]))
        }
    }
    df <- rbind(df, data.frame(col, content))
    
                          col
    1          Protein IDs
    2 Majority protein IDs
    3 Peptide counts (all)
    
    content
    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.
    

    Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2020-07-01
      • 1970-01-01
      相关资源
      最近更新 更多