I am a little bit puzzled when using lists of lists. If I try the following code, I get an error and I don't understand why:
library(tibble)
data <- list()
data[1][[1]] <- tibble(year = rep(2010,4)) # works
data[1][[2]] <- tibble(year = rep(2011,4)) # gives me an error
# Warning message:
# In data[1][[2]] <- tibble(year = rep(2011, 4)) :
# number of items to replace is not a multiple of replacement length
Any help on this, would be appreciated.
Try this:
data = list()
data[[1]] = list()
data[[1]][[1]] = tibble(year = rep(2010,4))
data[[1]][[2]] = tibble(year = rep(2011,4))
output:
[[1]]
[[1]][[1]]
# A tibble: 4 × 1
year
<dbl>
1 2010
2 2010
3 2010
4 2010
[[1]][[2]]
# A tibble: 4 × 1
year
<dbl>
1 2011
2 2011
3 2011
4 2011
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With