R 如何将向量写入数据表中同一行的几列中?

R 如何将向量写入数据表中同一行的几列中?,r,data.table,R,Data.table,我有一个空的数据表,希望将预定义的数值数据向量写入数据表的列子集(所有列都在同一行中)。这里有一个例子: require(data.table) mydf <- data.table(matrix(0, nrow = 13, ncol = 15)) input <- seq(2.22, 14.22, by = 1) set(mydf, 3L, 2L:14L, input) # Trying another form of writing into the data table my

我有一个空的数据表,希望将预定义的数值数据向量写入数据表的列子集(所有列都在同一行中)。这里有一个例子:

require(data.table)
mydf <- data.table(matrix(0, nrow = 13, ncol = 15))
input <- seq(2.22, 14.22, by = 1)
set(mydf, 3L, 2L:14L, input)
# Trying another form of writing into the data table 
mydf[3, 2:14] <- input
require(data.table)

mydf使用
as.list

library(data.table)
mydf[3, 2:14 := as.list(input)]
mydf

#   V1   V2   V3   V4   V5   V6   V7   V8   V9   V10   V11   V12   V13   V14 V15
#1:  0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00  0.00  0.00  0.00  0.00  0.00   0
#2:  0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00  0.00  0.00  0.00  0.00  0.00   0
#3:  0 2.22 3.22 4.22 5.22 6.22 7.22 8.22 9.22 10.22 11.22 12.22 13.22 14.22   0
#4:  0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00  0.00  0.00  0.00  0.00  0.00   0
#5:  0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00  0.00  0.00  0.00  0.00  0.00   0
#....
这也适用于
set

set(mydf, 3L, 2L:14L, as.list(input))