Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with Lists in R

Tags:

r

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.

like image 477
arnyeinstein Avatar asked Mar 12 '26 02:03

arnyeinstein


1 Answers

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
like image 151
VYago Avatar answered Mar 14 '26 15:03

VYago



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!