从嵌套列表中提取元素,并使用purrr作为(嵌套)数据帧的新列添加

时间:2019-04-02 17:38:23

标签: r purrr

我似乎无法弄清楚从嵌套结果列表中提取信息并把结果块作为主要小块的新列(嵌套小块)的purrr语法。

library(EnvStats)
library(tidyverse)

    #create example dataset
    df1 <- tibble(
      Site=rep(c("Site1", "Site2"), 3, each=12),
      Month=rep(c("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"), 6),
      Year=rep(c(2010, 2011, 2012), 2, each=12), 
      Value=sample(seq(from=0, to=20, by=.1), size=72, replace=TRUE)
    )

    df1 <- df1 %>% nest(-Site)

    res <- mutate(
      df1, 
      sk = map(data, ~ {
        EnvStats::kendallSeasonalTrendTest(.x$Value, season = .x$Month, 
                                           year = .x$Year)
      }))

主小节的结构以及嵌套的结果列表(被截断):

  glimpse(res)

    Observations: 2 Variables: 3    
    $ Site <chr> "Site1", "Site2"    
    $ data <list> [<tbl_df[36 x 3]>, <tbl_df[36 x 3]>]    
    $ sk   <list> [<4.9772727, 0.3049971, 11, 0.9322623, 0.7603683,   

    glimpse(res$sk)

    List of 2    
    $ :List of 15     
      ..$ estimate : Named num [1:3] -0.222 -1.2 1766.625
      .. ..- attr(*, "names")= chr [1:3] "tau" "slope" "intercept"
      ...more here 
    $ :List of 15          
        ..$ estimate : Named num [1:3] -0.222 -1.2 1766.625
        .. ..- attr(*, "names")= chr [1:3] "tau" "slope" "intercept"

我可以从sk列表中提取结果并将其作为新列放置在主标题中。例如:

#function to extract slope from a sk list
slope1 <- function(x) x$estimate[["slope"]]

#how to get results from nested list as a column in the main dataframe
ex1 <- res %>%  mutate(
      tau = map_dbl(sk, ~.x$estimate[["tau"]]),     
      slope = map_dbl(sk, slope1)
)

glimpse(ex1)

Observations: 2
Variables: 5
$ Site         <chr> "Site1", "Site2"
$ data         <list> [<tbl_df[36 x 3]>, <tbl_df[36 x 3]>]
$ sk           <list> [<4.9772727, 0.3049971, 11, 0.9322623, 0.7603683, 0.08333333, 0.47500000, -445.02500000, 0, 0,...
$ tau          <dbl> 0.08333333, -0.22222222
$ annual_slope <dbl> 0.475, -1.200

但是我无法弄清楚从sk列表中提取结果到新数据框,并将该数据框作为新列(每个站点1 df)附加到主res数据框中的语法。

寻找res df以新的EnvOut列结尾,如下所示:

$ Site         <chr> "Site1", "Site2"  
$ data         <list> [<tbl_df[36 x 3]>, <tbl_df[36 x 3]>]  
$ sk           <list> [<4.9772727, 0.3049971, 11, 0.9322623, 0.7603683,   
$ EnvOut       <list> [<tbl_df[A x B]>, …  

以下内容不起作用... purrr语法可能是一个问题,但我无法弄清楚。

#Now - how to create new column in res of (nested) dataframes with results extracted from original nested sk lists

#The dt_fun function will give me the dataframe for a single list 
#--> how to add this as nested list of dataframe to the original res dataframe?? 

df_fun <- function(df){
  df <- tibble(
    tau=df$estimate[["tau"]], 
    slope=slope1(df)
  )
  df
}

test <- df_fun(res$sk[[1]])


ex2  <- res %>%   mutate(
  envOut = map(sk, ~ dt_fun(.x[[1]]))
)

编辑: 下面的ex2和ex3似乎都可以工作。另外,要在不创建外部功能的情况下将结果添加到数据框,可以使用以下ex4。不确定这是最有效的语法-但它可以工作。

ex2  <- res %>%   mutate(
  envOut = map(sk, ~ df_fun(.x))
)

ex3  <- mutate(res, 
  envOut = map(sk, df_fun)
)

ex4  <- res %>%   mutate(
    envOut = map(sk, ~ tibble(tau = .x$estimate[["tau"]], 
                              slope = slope1(.x))
))

0 个答案:

没有答案