Make a list of items in x, regardless of x's type
make_list.Rd
Repeats x
as necessary to make n
of them.
Does not try to simplify x
.
Arguments
- x
The object to be duplicated.
- n
The number of times to be duplicated.
- lenx
The length of item
x
. Be default,lenx
is taken to belength(x)
,
Details
If x
is itself a vector or list,
you may want to override the default value for lenx
.
For example, if x
is a list that should be duplicated several times,
set lenx = 1
.
Examples
m <- matrix(c(1:6), nrow=3, dimnames = list(c("r1", "r2", "r3"), c("c2", "c1")))
make_list(m, n = 1)
#> [[1]]
#> c2 c1
#> r1 1 4
#> r2 2 5
#> r3 3 6
#>
make_list(m, n = 2)
#> [[1]]
#> c2 c1
#> r1 1 4
#> r2 2 5
#> r3 3 6
#>
#> [[2]]
#> c2 c1
#> r1 1 4
#> r2 2 5
#> r3 3 6
#>
make_list(m, n = 5)
#> [[1]]
#> c2 c1
#> r1 1 4
#> r2 2 5
#> r3 3 6
#>
#> [[2]]
#> c2 c1
#> r1 1 4
#> r2 2 5
#> r3 3 6
#>
#> [[3]]
#> c2 c1
#> r1 1 4
#> r2 2 5
#> r3 3 6
#>
#> [[4]]
#> c2 c1
#> r1 1 4
#> r2 2 5
#> r3 3 6
#>
#> [[5]]
#> c2 c1
#> r1 1 4
#> r2 2 5
#> r3 3 6
#>
make_list(list(c(1,2), c(1,2)), n = 4)
#> [[1]]
#> [1] 1 2
#>
#> [[2]]
#> [1] 1 2
#>
#> [[3]]
#> [1] 1 2
#>
#> [[4]]
#> [1] 1 2
#>
m <- matrix(1:4, nrow = 2)
l <- list(m, m+100)
make_list(l, n = 4)
#> [[1]]
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 101 103
#> [2,] 102 104
#>
#> [[3]]
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> [[4]]
#> [,1] [,2]
#> [1,] 101 103
#> [2,] 102 104
#>
make_list(l, n = 1) # Warning because l is trimmed.
#> Warning: n not evenly divisible by length(x)
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
make_list(l, n = 5) # Warning because length(l) (i.e., 2) not evenly divisible by 5
#> Warning: n not evenly divisible by length(x)
#> [[1]]
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> [[2]]
#> [,1] [,2]
#> [1,] 101 103
#> [2,] 102 104
#>
#> [[3]]
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
#> [[4]]
#> [,1] [,2]
#> [1,] 101 103
#> [2,] 102 104
#>
#> [[5]]
#> [,1] [,2]
#> [1,] 1 3
#> [2,] 2 4
#>
make_list(list(c("r10", "r11"), c("c10", "c11")), n = 2) # Confused by x being a list
#> [[1]]
#> [1] "r10" "r11"
#>
#> [[2]]
#> [1] "c10" "c11"
#>
make_list(list(c("r10", "r11"), c("c10", "c11")), n = 2, lenx = 1) # Fix by setting lenx = 1
#> [[1]]
#> [[1]][[1]]
#> [1] "r10" "r11"
#>
#> [[1]][[2]]
#> [1] "c10" "c11"
#>
#>
#> [[2]]
#> [[2]][[1]]
#> [1] "r10" "r11"
#>
#> [[2]][[2]]
#> [1] "c10" "c11"
#>
#>