【问题标题】:Append output from iterative mapped function附加迭代映射函数的输出
【发布时间】:2021-11-10 21:46:42
【问题描述】:

这是对上一个问题的跟进:Read functions as text and use for plotting

映射函数的输出...

data %>% 
  bind_cols(
    map(.x = models,.f = text_model) %>% 
      set_names(models) %>% 
      bind_rows(.id = "model") 
  )

...生成一个数据框,其中每个函数的结果写入单独的列(函数包含在列标题中)。

但是,最好附加每个函数的输出,以便所有结果都包含在同一列中,并使用单独的列来跟踪哪个函数(“model001”、“model002”、...” model500") 生成结果。

如何调整上一个问题(Read functions as text and use for plotting)的代码以这种方式编写结果?

编辑:有人建议Read functions as text and use for plotting 作为答案,但这篇文章是关于如何将输出写入单个列(而不是每个函数的单独列)的后续文章。

【问题讨论】:

标签: r function append


【解决方案1】:

鉴于其他答案,我们可以旋转数据

data %>% 
  bind_cols(
    map(.x = models,.f = text_model) %>% 
      set_names(models_names) %>% 
      bind_rows(.id = "model") 
  ) %>% 
  pivot_longer(cols = model1:model2,names_to = "model")

# A tibble: 200 x 6
       A       B      C       D model  value
   <dbl>   <dbl>  <dbl>   <dbl> <chr>  <dbl>
 1 0.833  0.538   0.647  1.65   model1  22.9
 2 0.833  0.538   0.647  1.65   model2  57.9
 3 2.07   1.20   -0.748 -2.04   model1  35.3
 4 2.07   1.20   -0.748 -2.04   model2  70.3
 5 0.880 -0.199   1.08   1.04   model1  29.2
 6 0.880 -0.199   1.08   1.04   model2  64.2
 7 0.252  0.400   1.45  -0.0645 model1  15.6
 8 0.252  0.400   1.45  -0.0645 model2  50.6
 9 0.746  0.0943 -1.00   1.44   model1  20.4
10 0.746  0.0943 -1.00   1.44   model2  55.4
# ... with 190 more rows

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多