根据一列中的唯一值创建多个数据框

时间:2020-07-08 15:30:11

标签: r

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

这是我正在使用的代码。

ViewControllerResolver { viewController in
    viewController.navigationItem.titleView = self.searchBar.searchController.searchBar
}

不是通过手动对所有50个州手动执行此过程,而是有一种更简单的方法通过执行以下操作来自动执行此操作吗?

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) 

运行该函数不会给我带来任何帮助或错误。

3 个答案:

答案 0 :(得分:0)

考虑//my code here return( <div> { products.map((card,index)=>{ return( <div className="product" key={index}> <div className="imgbox"> <img src={card.image} className="img1"></img> </div> <div className="specific"> <h6>{card.name}<br></br><span>{card.description}</span></h6> <div className="price">{card.price}</div> <label>Size</label> <ul> <li>20mm</li> <li>25mm</li> <li>30mm</li> </ul> <form> <div className="form-group"><label>Quantity</label><br /> <select> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> </select> </div> </form> <button className={this.state.button ? "buttonTrue": "buttonFalse"} onClick={this.handleClick}>Add to Cart</button> </div> </div> ) }) } </div> ) } } by的面向对象包装器),它按列中的一个或多个因素对数据帧进行子集化,并且(与tapply不同)将子集传递到已定义的方法中。 split的返回是所有等于等于唯一因子水平的方法输出的列表。

by

答案 1 :(得分:0)

您可以尝试以下方法:

#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)

答案 2 :(得分:0)

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

您也可以查看tibbletime/tsibble以留在tidyverse中。另外,请查看purrr软件包,以获取有关map系列函数的更多信息。

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创建