将函数的结果保存在返回计数和ggplot的数据框中

问题描述 投票:0回答:1

我编写了一个函数,它返回一个包含数字(odchylka.sr)和ggplot对象(ggpl)的列表

model.ARIMA.RF.fct <- function (l.obs.pomin) { 

  ...
  wynik.lst <- list (odchylka.sr, ggpl)
  return(wynik.lst)

}

我根据

将结果保存在数据框中
ARIMA.RF.cwu.wyniki <- data.frame(
  l.obs.pomin = c(52, 69, 136, 217)
  ) %>% 
  mutate(
    out = pmap(list (l.obs.pomin), model.ARIMA.RF.fct)
  )

收到了

enter image description here

我希望 odchylka.sr 成为“out”列之前的单独列。

我无法完成这项任务。

我使用 pmap 因为我的目标函数有 3 个参数

r dataframe ggplot2
1个回答
0
投票

function_that_returns_lists <- function(x){
  
  list(thing_1 = x+1,
       thing_2 = x^2)
}

df1 <- data.frame(
  l.obs.pomin = c(52, 69, 136, 217)
) %>% 
  mutate(
    out = pmap_df(list (l.obs.pomin), function_that_returns_lists) # pmap_df so out is more conveniently structured
  ) |> unnest(out) # easily seperate out by its columns into the dataset
© www.soinside.com 2019 - 2024. All rights reserved.