将矩阵分配给data.table的子集 [英] Assign a matrix to a subset of a data.table
问题描述
我想为一个 data.table
的多列子集分配一个矩阵,但该矩阵最终被视为一个列向量。例如,
I would like to assign a matrix to a multi-column subset of a data.table
but the matrix ends up getting treated as a column vector. For example,
dt1 <- data.table(a1=rnorm(5), a2=rnorm(5), a3=rnorm(5))
m1 <- matrix(rnorm(10), ncol=2)
dt1[,c("a1","a2")] <- m1
Warning messages:
1: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, :
2 column matrix RHS of := will be treated as one vector
2: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, :
Supplied 10 items to be assigned to 5 items of column 'a1' (5 unused)
3: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, :
2 column matrix RHS of := will be treated as one vector
4: In `[<-.data.table`(`*tmp*`, , c("a1", "a2"), value = c(-0.308851784175091, :
Supplied 10 items to be assigned to 5 items of column 'a2' (5 unused)
问题可以通过先转换 m1
是另一个 data.table
对象,但我很好奇这个错误的原因。如果 dt1
是 data.frame
;没有使用 data.table
工作的架构理由是什么?
The problem can be solved by first converting m1
to be another data.table
object, but I'm curious what the reasonsing is for this error. The above syntax would work if dt1
were a data.frame
; what is the architectural rationale for not having it work with data.table
?
推荐答案
data.frame
不是 matrix
,也不是 data.table
a matrix
。 data.frame
和 data.table
对象列表
。
A data.frame
is not a matrix
, nor is a data.table
a matrix
. Both data.frame
and data.table
objects are lists
. These are stored very differently, although the indexing can be similar, this is processed under the hood.
[< - 。data.frame
将矩阵值值
分隔为每列包含元素的列表。
Within [<-.data.frame
splits a matrix-valued value
into a list with an element for each column.
( value <-split(value,col(value))
)。
在将一些东西分配给列子集的过程中, [< - 。data.frame
]将复制整个data.frame。
Note also that [<-.data.frame
will copy the entire data.frame in the process of assigning something to a subset of columns.
data.table
尝试避免此复制,如 [<。。data.table $ c $因为
R
中的所有< -
方法都可以复制。
data.table
attempts to avoid this copying, as such [<-.data.table
should be avoided, as all <-
methods in R
make copies.
[< - 。data.table
, [< - 。data.frame $ c $如果
i
是一个矩阵,而不是 value
是
data.table
通常喜欢你明确地确保在分配时数据的类型匹配。
data.table
usually likes you to be explicit in ensuring that the types of data match when assigning. This helps avoid any coercion and related copying.
您可以放入功能请求这里,以确保兼容性,但鉴于您的使用远远超出了建议,那么也许包的作者可能会请求您使用数据。表
约定和方法。
You could, perhaps put in a feature request here to ensure compatibility, but given your usage is far outside what is recommended, then perhaps the package authors might request you simply use the data.table
conventions and approaches.
这篇关于将矩阵分配给data.table的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!