data table avoid processing

I create data.tablefrom two (or more) input vectors of different lengths:

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

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

And you need a shorter vector to be filled in NAinstead of data.tableusing their values, resulting in a data.tablesimilar one to this:

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

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

Thank!

+6
source share
3 answers

You can use indices out of range:

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 expand yby doing (like @Roland recommended in the comment):

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

Option cbind.fillfromrowr

library(rowr)
setNames(cbind.fill(x, y, fill = NA), c("x", "y"))

vector list, NA list

library(data.table)
lst <- list(x = x, y = y)
as.data.table(lapply(lst, `length<-`, max(lengths(lst))))
#   x  y
#1: 1  8
#2: 2  9
#3: 3 NA
#4: 4 NA
+2

The out-of-range indexes answer provided by jogo can simply be extended for in-place assignment with .N:

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])

z <- c(6,7)
dt[, z := z[1:.N]]
#    x  y  z
# 1: 1  8  6
# 2: 2  9  7
# 3: 3 NA NA
# 4: 4 NA NA
0
source

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


All Articles