【问题标题】:Reading multiple tables from one tsv file to an R dataframe将多个表从一个 tsv 文件读取到 R 数据帧
【发布时间】:2021-02-04 11:01:52
【问题描述】:

我想在 R 中从 github 读取数据。这是我的代码。

library(tidyverse)
cluster_tables <- read_tsv("https://raw.githubusercontent.com/hodcroftlab/covariants/master/cluster_tables/all_tables.tsv", skip_empty_rows = T)

它只读取第一列,不显示其余列。如何将此数据集作为 R 中的一个数据框?另外,有没有办法在这个页面上创建一个带有井号表名的列?

【问题讨论】:

    标签: r dplyr readr


    【解决方案1】:

    skip = 4读取数据

    cluster_tables <- readr::read_tsv("https://raw.githubusercontent.com/hodcroftlab/covariants/master/cluster_tables/all_tables.tsv", skip = 4, skip_empty_rows = TRUE)
    head(cluster_tables)
    
    #   X1             first_seq  num_seqs last_seq  
    #  <chr>          <chr>      <chr>    <chr>     
    #1 Netherlands    2020-06-20 1615     2021-01-21
    #2 Spain          2020-06-20 2003     2021-01-12
    #3 United Kingdom 2020-07-07 69421    2021-01-23
    #4 Belgium        2020-07-17 384      2021-01-20
    #5 Switzerland    2020-07-22 1706     2021-01-19
    #6 Ireland        2020-07-23 603      2021-01-22
    

    由于页面上有多个表格可以在一个数据框中自动读取它们,我们可以进行一些操作。

    • readLines读取数据
    • 删除所有空行
    • 只要遇到'##',就将数据集拆分为一个新列表。
    • 对于每个列表,将第一个值(即表的名称)分开并将其添加为新列。
    • 将数据帧列表合并到一个大数据帧 (result)。
    tmp <- readLines('https://raw.githubusercontent.com/hodcroftlab/covariants/master/cluster_tables/all_tables.tsv')
    tmp <- tmp[tmp != '']
    
    do.call(rbind, lapply(split(tmp, cumsum(grepl('##', tmp))), function(x) {
      name <- sub('##\\s+', '', x[1])
      x <- x[-1]
      transform(read.csv(text = paste0(x, collapse = '\n'), sep = '\t'), name = name)
    })) -> result
    
    head(result)
    #                 X  first_seq num_seqs   last_seq    name
    #1.1    Netherlands 2020-06-20     1615 2021-01-21 20A.EU1
    #1.2          Spain 2020-06-20     2003 2021-01-12 20A.EU1
    #1.3 United Kingdom 2020-07-07    69421 2021-01-23 20A.EU1
    #1.4        Belgium 2020-07-17      384 2021-01-20 20A.EU1
    #1.5    Switzerland 2020-07-22     1706 2021-01-19 20A.EU1
    #1.6        Ireland 2020-07-23      603 2021-01-22 20A.EU1
    

    【讨论】:

    • 谢谢!这些是一页上的多个数据框。有没有办法在这个页面上创建一个带有这些标签表名的列?
    • 我没有注意到这一点。您应该在主要问题中添加了有关名称的信息。我已经更新了答案,以显示如何在一个具有名称的数据框中读取它们。
    • 抱歉给您带来了困惑,更新了问题。感谢您的更新答案!
    【解决方案2】:

    从上述答案中的Ronak Shah 获得灵感,我尝试使用 tidyverse

    library(tidyverse)    
    
    cluster_tables <- readLines('https://raw.githubusercontent.com/hodcroftlab/covariants/master/cluster_tables/all_tables.tsv')
    
    cluster_tables %>% 
      as_tibble() %>% 
      separate(value, into = c("countries", "first_seq", "num_seqs", "last_seq"), sep = "\t") %>% 
      filter(countries != "") %>% 
      mutate(variants = if_else(str_detect(countries, "## "), countries, NA_character_)) %>% 
      fill(variants, .direction = "down") %>% 
      filter(!is.na(first_seq))
    
    head(cluster_tables)
    # A tibble: 6 x 5
      countries      first_seq  num_seqs last_seq   variants  
      <chr>          <chr>      <chr>    <chr>      <chr>     
    1 Netherlands    2020-06-20 1615     2021-01-21 ## 20A.EU1
    2 Spain          2020-06-20 2003     2021-01-12 ## 20A.EU1
    3 United Kingdom 2020-07-07 69421    2021-01-23 ## 20A.EU1
    4 Belgium        2020-07-17 384      2021-01-20 ## 20A.EU1
    5 Switzerland    2020-07-22 1706     2021-01-19 ## 20A.EU1
    6 Ireland        2020-07-23 603      2021-01-22 ## 20A.EU1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2012-06-28
      相关资源
      最近更新 更多