How do I use the map2 function (in the purrr package) on a subset of columns without getting the error that "mapped vectors must have consistent lengths"? The number of subset columns equals the length of the other vector.
Here's my data:
library(tidyverse)
library(rstatix)
df <- tibble::tribble(
~id, ~edge, ~trt, ~nl, ~lm, ~md, ~c, ~mgg, ~mgcm, ~p, ~sp, ~ap, ~la, ~lacm, ~lacmd,
1L, "S", "C", 1.802500944, -1.126394361, 1.747757193, -0.302911966, 2.942376992, 1.01978392, 1.603508872, 1166.214587, 1.104182097, 3.630403855, 0.925433649, 2.083967271,
2L, "S", "T", NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
3L, "D", "C", 1.59505822, -1.554475881, 1.173922711, -0.340665184, 1.854642163, 0.787036727, 1.40878277, 663.125567, 0.898799413, 3.332281129, 0.803131628, 1.841247752,
4L, "D", "T", 1.342572531, -2.21548947, 0.961702527, -0.331617331, 1.645569808, 0.750246559, 0.67441638, 63.63830862, 0.542966439, 2.416127169, 0.574963833, 1.529239129,
5L, "S", "C", 1.802500944, -0.165110402, 2.162768585, -0.285160482, 3.136984959, 1.093832043, 2.209960854, 495.84715, 1.164570199, 4.799142774, 0.994423991, 2.034565573,
6L, "S", "T", 2.141481291, 0.250369615, 2.439863935, -0.218925863, 3.358891011, 1.163094493, 2.30563155, 910.5631088, 1.198833277, 5.219802305, 1.097357732, 2.149000371
)
head(df)
#> # A tibble: 6 x 15
#> id edge trt nl lm md c mgg mgcm p sp ap
#> <int> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 S C 1.80 -1.13 1.75 -0.303 2.94 1.02 1.60 1166. 1.10
#> 2 2 S T NA NA NA NA NA NA NA NA NA
#> 3 3 D C 1.60 -1.55 1.17 -0.341 1.85 0.787 1.41 663. 0.899
#> 4 4 D T 1.34 -2.22 0.962 -0.332 1.65 0.750 0.674 63.6 0.543
#> 5 5 S C 1.80 -0.165 2.16 -0.285 3.14 1.09 2.21 496. 1.16
#> 6 6 S T 2.14 0.250 2.44 -0.219 3.36 1.16 2.31 911. 1.20
#> # ... with 3 more variables: la <dbl>, lacm <dbl>, lacmd <dbl>
I have successfully run the map function to create a list of 12 models (let's call it "models_1"):
models_1 <- map(df[,4:15], ~(lm(.x ~df$edge*df$trt)))
Now, I would like to use map2 to create second set of models which refer to the first set of models I created. I keep getting the error that "mapped vectors must have consistent lengths", even though the number of columns I'm calling in map2 equals the number of models in "models_1":
sim <- df %>%
group_by(edge) %>%
map2(df[, 4:15], models_1, ~(anova_test(.x~ df$trt, error = .y, type = 3)))
I get the following error:
Error: Mapped vectors must have consistent lengths:
* `.x` has length 15
* `.y` has length 12
Here is a working example of what I'd like to do (this is only for one model, but I have 12+ so I'd like to make it automated if possible):
model <- lm(nl ~ edge*trt, data = df)
df %>%
group_by(edge) %>%
anova_test(nl ~ trt, error = model, type = 3)
Created on 2020-05-19 by the reprex package (v0.3.0)
%>%and then namedfin the first argument ofmap2, this defeats the purpose ofgroup_by. Use.instead. Second,map2needs arguments of the same length. One option is to usecross2. Third, if you want togroup_byand usemap, you'll need to usenest. Often it's easier to just usesplitfrom base R. - Ian Campbell.rather thandfinmap2. However, I am at a loss after that. I might be missing something, but it seems thatcross2is used to multiply.xand.ywhich is not quite what I'm looking for. I can't simply remove all unwanted to columns from my dataframe first, because I need to leave thedf$trtcolumn in order to run the second function. I'm sure this is elemental, but I'm relatively new to R and at the limits of my knowledge here... Thanks! - DMC