Adding columns to a data table

后端 未结 2 920
慢半拍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:33

    I think you want cbind

    cbind(dt, df[, 1:2])
    # fieldX field1 field2
    # 1      x      1      2
    # 2      x      1      2
    # 3      x      1      2
    
    0 讨论(0)
  • 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]]
    
    0 讨论(0)
提交回复
热议问题