Alex808 Alex808 - 2 months ago 16
R Question

How to create data.table with different size of columns

I create data.table like this.

DT = data.table(A=c(1,2,3), B=c(1,2,3,4,5))


But I get this result.

A B
1: 1 1
2: 2 2
3: 3 3
4: 1 4
5: 2 5


but I'd like to get this.

A B
1: 1 1
2: 2 2
3: 3 3
4: NA 4
5: NA 5


How can I create data.table with different size?

Answer
install.packages("qpcR")
library(qpcR)

df <- data.table(qpcR:::cbind.na(A=c(1,2,3), B=c(1,2,3,4,5)))
  A B
1  1 1
2  2 2
3  3 3
4 NA 4
5 NA 5