r - How to create a data.table with one row filled with NA based on another data.table -
say have following data.table
(but think of table lot of columns dynamically changing names)
dt <- data.table(a=rep(1l,3),b=rep(1.32,3),d=rep("qwe",3)) dt b d 1: 1 1.32 qwe 2: 1 1.32 qwe 3: 1 1.32 qwe
say want create row nas
rbindlist
dt.
first try (it not working)
dt1 <- dt[1][,colnames(dt):=na] dt1 b d 1: na na na rbindlist(list(dt1,dt)) b d 1: na na na 2: true true na 3: true true na 4: true true na
this not working because dt1
casted when :=na
called (something called plonking seems, because if provide full column when :=
rhs type overwrite lhs...)
the question then, how can extract row of data.table
, fill na or create data.table
, filled na have same column names , column type another
there many ways of doing it, here's one:
rbind(dt[na], dt) # b d #1: na na na #2: 1 1.32 qwe #3: 1 1.32 qwe #4: 1 1.32 qwe
Comments
Post a Comment