Adding columns to a data table

后端 未结 2 926
慢半拍i
慢半拍i 2020-12-31 02:23

I have a data.frame (or a matrix or any other tabular data structure object for that matter):

df = data.frame(field1 = c(1,1,1),field2 = c(2,2,2),field3 = c(         


        
2条回答
  •  一个人的身影
    2020-12-31 02:57

    You can use cbind:

    cbind(dt, df[fields])
    

    However, the most efficient way is still probably going to be to use data.table's assign by reference:

    dt[, (fields) := df[fields]]
    

提交回复
热议问题