Create lists in R with patterns associated with a record number

Is there a way to create a list similar to the one below in R, using perhaps lapply()other more extrapolated procedures?

ones   =   c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
twos   =   c(1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1)
threes =   c(1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0)
fours  =   c(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0)
fives  =   c(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)
l = list(ones, twos, threes, fours)

[[1]]
 [1] 1 1 1 1 1 1 1 1 1 1 1

[[2]]
 [1] 1 0 1 0 1 0 1 0 1 0 1

[[3]]
 [1] 1 0 0 1 0 0 1 0 0 1 0

[[4]]
 [1] 1 0 0 0 1 0 0 0 1 0 0

They correspond to the coefficients of polynomials in the generating functions for partitions.

The first list is for ones, and therefore the calculation is performed in increments 1of an integer; therefore, the vector 1,1,1,1,1,1,1,...In the record [[2]]we have twos, and we look forward to 2starting with 0, skipping 1(encoded as 0). In [[3]]we are counting on 3's: zero, three, six, nine, etc.

+4
source share
1 answer

R

lapply(seq(0L, 5L), function(i) rep(c(1L, integer(i)), length.out=11L))
[[1]]
 [1] 1 1 1 1 1 1 1 1 1 1 1

[[2]]
 [1] 1 0 1 0 1 0 1 0 1 0 1

[[3]]
 [1] 1 0 0 1 0 0 1 0 0 1 0

[[4]]
 [1] 1 0 0 0 1 0 0 0 1 0 0

[[5]]
 [1] 1 0 0 0 0 1 0 0 0 0 1
  • seq(0L, 5L) 0 5, seq_len(5L)-1L, .
  • c(1L, integer(i)) 0-1, rep ( 11), length.out.
  • lapply function(i) 0s .
+7

Source: https://habr.com/ru/post/1666408/


All Articles