问题
我有两篇针对不同模型的参数的小标题,我希望找到一种方法来生成这些模型的前80个输出的列表的列表列。这里的示例是100个模型,但是将来,我将研究10,000多个模型,这就是为什么我希望将它们的输出与它们连续分组的原因。
我尝试过的事情
library(tidyverse)
new.tibble <- tibble(a = rep(1:5, 20))
new.tibble <- new.tibble %>% add_column(b = 1:100)
my.vector <- c(1:80)
# What I want
new.tibble <- new.tibble %>% mutate(c = lapply(my.vector, function(x) {a ^ (x / b)}))
我已经用different apply functions尝试了此操作(适用,适用,适用等),但它似乎不起作用。当我运行最后一行时,得到以下内容:
> new.tibble <- new.tibble %>% mutate(c = lapply(my.vector, function(x) {a ^ (x / b)}))
Error: Column `c` must be length 100 (the number of rows) or one, not 80
这使我相信,该mutate总共仅生成80个输出,而不是每行80个输出,并且按照我的意愿将这80个输出存储在该行的列表中。我尝试让我的小标题按行排列,看看是否有帮助:
> row.tibble <- rowwise(new.tibble)
> row.tibble <- row.tibble %>% mutate(c = lapply(my.vector, function(x){a ^ (x / b)}))
Error: Column `c` must be length 1 (the group size), not 80
它没有。我知道建立一个while循环并不难,只需生成不同的输出作为自己的单独列表即可,但是有10,000多个列表,每个列表对应于行中的模型,我想列表列将是组织输出的最佳方法。我也尝试过使用as.list强制返回列表的输出,但这并没有达到我的预期:
> row.tibble <- row.tibble %>% mutate(c = as.list(lapply(my.vector, FUN = function(x){a ^ (x / b)})))
Error: Column `c` must be length 1 (the group size), not 80
> new.tibble <- new.tibble %>% mutate(c = as.list(lapply(my.vector, FUN = function(x){a ^ (x / b)})))
Error: Column `c` must be length 100 (the number of rows) or one, not 80
我尝试避开不适,并尝试直接获得所需的输出,但这没有用:
> new.tibble %>% mutate(c = as.list(a ^ (my.vector / b)))
# A tibble: 100 x 3
a b c
<int> <int> <list>
1 1 1 <dbl [1]>
2 2 2 <dbl [1]>
3 3 3 <dbl [1]>
4 4 4 <dbl [1]>
5 5 5 <dbl [1]>
6 1 6 <dbl [1]>
7 2 7 <dbl [1]>
8 3 8 <dbl [1]>
9 4 9 <dbl [1]>
10 5 10 <dbl [1]>
# ... with 90 more rows
Warning message:
In my.vector/b :
longer object length is not a multiple of shorter object length
> row.tibble %>% mutate(c = as.list(a ^ (my.vector / b)))
Error: Column `c` must be length 1 (the group size), not 80
其他信息
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] forcats_0.5.0 stringr_1.4.0 dplyr_0.8.5 purrr_0.3.4 readr_1.3.1 tidyr_1.0.3 tibble_3.0.1 ggplot2_3.3.0 tidyverse_1.3.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 cellranger_1.1.0 pillar_1.4.4 compiler_4.0.0 dbplyr_1.4.3 tools_4.0.0 jsonlite_1.6.1 lubridate_1.7.8 lifecycle_0.2.0
[10] nlme_3.1-147 gtable_0.3.0 lattice_0.20-41 pkgconfig_2.0.3 rlang_0.4.6 reprex_0.3.0 cli_2.0.2 DBI_1.1.0 rstudioapi_0.11
[19] haven_2.2.0 withr_2.2.0 xml2_1.3.2 httr_1.4.1 fs_1.4.1 generics_0.0.2 vctrs_0.2.4 hms_0.5.3 grid_4.0.0
[28] tidyselect_1.0.0 glue_1.4.0 R6_2.4.1 fansi_0.4.1 readxl_1.3.1 modelr_0.1.7 magrittr_1.5 backports_1.1.6 scales_1.1.1
[37] ellipsis_0.3.0 rvest_0.3.5 assertthat_0.2.1 colorspace_1.4-1 utf8_1.1.4 stringi_1.4.6 munsell_0.5.0 broom_0.5.6 crayon_1.3.4
答案 0 :(得分:1)
您是否正在寻找这种结果?
new.tibble <- new.tibble %>%
mutate(c = map2(.x = a, .y = b, .f = ~.x^(my.vector/.y) ))
输出:
head(new.tibble)
# A tibble: 6 x 3
a b c
<int> <int> <list>
1 1 1 <dbl [80]>
2 2 2 <dbl [80]>
3 3 3 <dbl [80]>
4 4 4 <dbl [80]>
5 5 5 <dbl [80]>
6 1 6 <dbl [80]>