Search code examples
rdatagriddata-manipulation

R: 'from' must be of length 1


I am working with the R programming language.

I am trying to define a grid using the "expand.grid" command, where the grid "self references" itself:

random_1 <- seq(80,100,5)
random_2 <- seq(random_1,120,5)
random_3 <- seq(85,120,5)
random_4 <- seq(random_3,120,5)
split_1 =  seq(0,1,0.5)
split_2 =  seq(0,1,0.5)
split_3 =  seq(0,1,0.5)
DF_2 <- expand.grid(random_1 , random_2, random_3, random_4, split_1, split_2, split_3)

But this returns the following error:

Error in seq.default(random_3, 120, 5) : 'from' must be of length 1
>

Does anyone know how to fix this error?

Thanks


Solution

  • seq takes a single element as from, to. According to ?seq

    from, to - the starting and (maximal) end values of the sequence. Of length 1 unless just from is supplied as an unnamed argume

    We could loop over the 'random_1'

    lapply(random_1, function(x) seq(x, 120, 5))
    

    Do the same for random_3 and construct as

    > random_2 <- lapply(random_1, function(x) seq(x, 120, 5))
    > random_3 <- seq(85,120,5)
    > random_4 <- lapply(random_3, function(x) seq(x, 120, 5))
    > split_1 =  seq(0,1,0.5)
    > split_2 =  seq(0,1,0.5)
    > split_3 =  seq(0,1,0.5)
    > DF_2 <- expand.grid(c(list(random_1), random_2, random_3, random_4, list(split_1, split_2, split_3)))