Home > Mobile >  Using nested purrr map functions as alternative code
Using nested purrr map functions as alternative code

Time:09-13

I have the following code that contains two vectors of different lengths, and repeats each column so that when the vectors are column bound, there is a unique combination for each row of the the new dataframe. My question is this: Is there a way to achieve the same outcome from the code below by using nested purrr::map functions?

library(tidyverse)

big <- LETTERS[1:10]
small <- letters[1:6]

ColBig <- rep(big, each =length(small))
ColSmall <- rep(small, length(big))

bind_cols(ColBig, ColSmall) %>% 
  setNames(c("ColBig", "ColSmall"))

CodePudding user response:

Probably something like this if you would like to use map

> do.call(rbind, map(as.list(big), ~ data.frame(ColBig = .x, ColSmall = small)))
   ColBig ColSmall
1       A        a
2       A        b
3       A        c
4       A        d
5       A        e
6       A        f
7       B        a
8       B        b
9       B        c
10      B        d
11      B        e
12      B        f
13      C        a
14      C        b
15      C        c
16      C        d
17      C        e
18      C        f
19      D        a
20      D        b
21      D        c
22      D        d
23      D        e
24      D        f
25      E        a
26      E        b
27      E        c
28      E        d
29      E        e
30      E        f
31      F        a
32      F        b
33      F        c
34      F        d
35      F        e
36      F        f
37      G        a
38      G        b
39      G        c
40      G        d
41      G        e
42      G        f
43      H        a
44      H        b
45      H        c
46      H        d
47      H        e
48      H        f
49      I        a
50      I        b
51      I        c
52      I        d
53      I        e
54      I        f
55      J        a
56      J        b
57      J        c
58      J        d
59      J        e
60      J        f

but I guess expand.grid from base R should be enough

> rev(expand.grid(ColSmall = small, ColBig = big))
   ColBig ColSmall
1       A        a
2       A        b
3       A        c
4       A        d
5       A        e
6       A        f
7       B        a
8       B        b
9       B        c
10      B        d
11      B        e
12      B        f
13      C        a
14      C        b
15      C        c
16      C        d
17      C        e
18      C        f
19      D        a
20      D        b
21      D        c
22      D        d
23      D        e
24      D        f
25      E        a
26      E        b
27      E        c
28      E        d
29      E        e
30      E        f
31      F        a
32      F        b
33      F        c
34      F        d
35      F        e
36      F        f
37      G        a
38      G        b
39      G        c
40      G        d
41      G        e
42      G        f
43      H        a
44      H        b
45      H        c
46      H        d
47      H        e
48      H        f
49      I        a
50      I        b
51      I        c
52      I        d
53      I        e
54      I        f
55      J        a
56      J        b
57      J        c
58      J        d
59      J        e
60      J        f

CodePudding user response:

You can try:

map_df(big, ~map(small, ~c(ColBig = .y, ColSmall = .x), .y = .x))

# # A tibble: 60 x 2
# ColBig ColSmall
# <chr>  <chr>   
# 1 A      a       
# 2 A      b       
# 3 A      c       
  • Related