rdata.tablerecycle

data.table avoid recycling


I'm constructing a data.table from two (or more) input vectors with different lengths:

x <- c(1,2,3,4)
y <- c(8,9)

dt <- data.table(x = x, y = y)

And need the shorter vector(s) to be filled with NA rather than recycling their values, resulting in a data.table like this:

   x  y
1: 1  8
2: 2  9
3: 3 NA
4: 4 NA

Is there a way to achieve this without explicitly filling the shorter vector(s) with NA before passing them to the data.table() constructor?

Thanks!


Solution

  • One can use out of range indices:

    library("data.table")
    
    x <- c(1,2,3,4)
    y <- c(8,9)
    n <- max(length(x), length(y))
    
    dt <- data.table(x = x[1:n], y = y[1:n])
    # > dt
    #    x  y
    # 1: 1  8
    # 2: 2  9
    # 3: 3 NA
    # 4: 4 NA
    

    Or you can extend y by doing (as @Roland recommended in the comment):

    length(y) <- length(x) <- max(length(x), length(y))
    dt <- data.table(x, y)