【问题标题】:Create multiple dataframes based on unique values in a column根据列中的唯一值创建多个数据框
【发布时间】:2020-07-08 15:30:11
【问题描述】:

您好,我正在尝试对数据进行子集化以声明特定的数据帧,然后将它们转换为 XTS 对象。

这是我正在使用的代码。

library(tidyverse)

us <- read_csv(url("https://covidtracking.com/api/v1/states/daily.csv")) # pulling detailed state level data from covidtracking project website
us <- select(us, date, state, cases = positive, hosp = hospitalizedCumulative, icu = inIcuCumulative, death) # selecting columns of interest
head(us)

AK <- filter(us, state == "AK") 
AK <- xts(AK[,c(-1,-2)], order.by = strptime(AK$date, format = "%Y%m%d")) %>% na.fill(0) 

除了手动为所有 50 种状态逐一执行此过程之外,有没有更简单的方法可以通过执行此类操作来自动执行此过程?

state <- unique(us$state)

x <- function(x) {
  x <- filter(us, state == x)
  x <- xts(x[,c(-1,-2)], order.by = strptime(x$date, format = "%Y%m%d")) %>% na.fill(0)
}

运行该函数没有给我任何结果或错误。

【问题讨论】:

    标签: r


    【解决方案1】:

    考虑bytapply 的面向对象的包装器),它通过列中的一个或多个因子对数据帧进行子集化,并且(与split 不同)将子集传递到定义的方法中。 by 的返回是任何方法输出的列表,该列表等于因子的唯一级别数。

    build_xts <- function(x) {
      xts(x[,c(-1,-2)], order.by = strptime(x$date, format = "%Y%m%d")) %>% na.fill(0)
    }
    
    xts_list <- by(us, us$state, build_xts)
    
    xts_list$AK
    ...
    xts_list$WY
    

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      #List
      List <- split(us,us$state)
      #Process
      process <- function(x)
      {
        x[is.na(x)]<-0
        y <- xts::xts(x[,c(-1,-2)], order.by = strptime(x$date, format = "%Y%m%d"))
        return(y)
      }
      
      List2 <- lapply(List,process)
      

      【讨论】:

        【解决方案3】:

        这种方法会创建一列从每个州的数据创建的 xts 对象。它有点像group_bysummarize,但输出是对象列表而不是原子向量。我选择使用 tidyverse 动词进行子集化,但您也可以使用 [[

        您还可以查看tibbletime/tsibble 以留在tidyverse。另请查看purrr 包,了解有关地图系列函数的更多信息。

        library(tidyverse)
        library(lubridate)
        library(xts)
        
        # pulling detailed state level data from covidtracking project website
        us <- read_csv(url("https://covidtracking.com/api/v1/states/daily.csv")) %>%
            select(date,
                   state,
                   cases = positive,
                   hosp = hospitalizedCumulative,
                   icu = inIcuCumulative,
                   death) 
        
        # Create a function that will take a tibble and return an XTS object
        build_xts <- function(df) {
            out <- xts(select(df, -c(1:2)),
                       order.by = pull(df, "date"))
            return(out)   
        }
        
        us %>% 
            mutate(date = strptime(date, format = "%Y%m%d")) %>% 
            # Create a list-column with the relevant data per state
            nest(data = c(date, cases, hosp, icu, death)) %>% 
            
            # Apply the "build_xts" function to each one
            mutate(time_obj = map(
                .x = data,
                .f = build_xts))
        
        #> # A tibble: 56 x 3
        #>    state data               time_obj       
        #>    <chr> <list>             <list>         
        #>  1 AK    <tibble [124 x 5]> <xts [124 x 3]>
        #>  2 AL    <tibble [123 x 5]> <xts [123 x 3]>
        #>  3 AR    <tibble [124 x 5]> <xts [124 x 3]>
        #>  4 AS    <tibble [114 x 5]> <xts [114 x 3]>
        #>  5 AZ    <tibble [126 x 5]> <xts [126 x 3]>
        #>  6 CA    <tibble [126 x 5]> <xts [126 x 3]>
        #>  7 CO    <tibble [125 x 5]> <xts [125 x 3]>
        #>  8 CT    <tibble [123 x 5]> <xts [123 x 3]>
        #>  9 DC    <tibble [125 x 5]> <xts [125 x 3]>
        #> 10 DE    <tibble [124 x 5]> <xts [124 x 3]>
        #> # ... with 46 more rows
        

        reprex package (v0.3.0) 于 2020-07-08 创建

        【讨论】:

          猜你喜欢
          • 2021-01-22
          • 2021-10-30
          • 2023-02-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-20
          相关资源
          最近更新 更多