In which I continue to be confused about ::unlist, but manage to join two lists, as required
first_list <-
list(
a = letters[1:3],
b = "mittens",
c = c(TRUE, FALSE),
d = rnorm(4)
)
second_list <-
list(
e = "buttons",
f = letters[1:5],
g = runif(2)
)
# if I have a list(list, list) how do I get list of the elmenets of hte list?
# Elements of first list as elements,
# and elements of second list as elements?
unlist(first_list, second_list) %>%
dim()
NULL
[1] "character"
# character?!
unlist(first_list, second_list)
a1 a2 a3
"a" "b" "c"
b c1 c2
"mittens" "TRUE" "FALSE"
d1 d2 d3
"-0.658853976666822" "-1.29571869155379" "-0.541580614619811"
d4
"-0.150846740435645"
# oh it made everything flat, that's not what I want. I still want the elements
# to remain lists, what about this recursive argument?
unlist(first_list, second_list, recursive = FALSE)
a1 a2 a3
"a" "b" "c"
b c1 c2
"mittens" "TRUE" "FALSE"
d1 d2 d3
"-0.658853976666822" "-1.29571869155379" "-0.541580614619811"
d4
"-0.150846740435645"
# okay, maybe I'm overthinking this, what happens if I make a list of two lists
list(first_list, second_list)
[[1]]
[[1]]$a
[1] "a" "b" "c"
[[1]]$b
[1] "mittens"
[[1]]$c
[1] TRUE FALSE
[[1]]$d
[1] -0.6588540 -1.2957187 -0.5415806 -0.1508467
[[2]]
[[2]]$e
[1] "buttons"
[[2]]$f
[1] "a" "b" "c" "d" "e"
[[2]]$g
[1] 0.08550033 0.32802539
List of 2
$ :List of 4
..$ a: chr [1:3] "a" "b" "c"
..$ b: chr "mittens"
..$ c: logi [1:2] TRUE FALSE
..$ d: num [1:4] -0.659 -1.296 -0.542 -0.151
$ :List of 3
..$ e: chr "buttons"
..$ f: chr [1:5] "a" "b" "c" "d" ...
..$ g: num [1:2] 0.0855 0.328
# see I have a list of 4 and a list of 3, I want a list of 7, not a list of 2
# comprising 4 and 3 ugh
# can I use map to extract the elements?
map(first_list, 1)
$a
[1] "a"
$b
[1] "mittens"
$c
[1] TRUE
$d
[1] -0.658854
# how is this different from first list?
first_list
$a
[1] "a" "b" "c"
$b
[1] "mittens"
$c
[1] TRUE FALSE
$d
[1] -0.6588540 -1.2957187 -0.5415806 -0.1508467
# oh that's not what I want
# fuck me, I'm going to look this up on the tubes now.
And switch to a post. This is proving to be more involved than I thought.
This post on stackoverflow suggests using ::append
, despite the function saying it is for vectors. Worth a try.
append(first_list, second_list)
$a
[1] "a" "b" "c"
$b
[1] "mittens"
$c
[1] TRUE FALSE
$d
[1] -0.6588540 -1.2957187 -0.5415806 -0.1508467
$e
[1] "buttons"
$f
[1] "a" "b" "c" "d" "e"
$g
[1] 0.08550033 0.32802539
# compare with
first_list
$a
[1] "a" "b" "c"
$b
[1] "mittens"
$c
[1] TRUE FALSE
$d
[1] -0.6588540 -1.2957187 -0.5415806 -0.1508467
second_list
$e
[1] "buttons"
$f
[1] "a" "b" "c" "d" "e"
$g
[1] 0.08550033 0.32802539
Huh, well, there you go. So a base function to the rescue.