You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

为何数据表整数矩阵乘法可行,浮点数版本却无法运行?

Why One Code Snippet Works, the Other Doesn't

Let’s break this down step by step, focusing on how matrix indexing rules and data.table's column iteration interact here.

The Working Code: Integer Values as Indices

First, let's look at the code that runs successfully:

a <- data.table(t(c(1, 2, 3, 4, 5)))
b <- matrix(data=1, nrow=10, ncol=5)
a[,lapply(.SD,function(x)(x*b[,x]))]

Here’s what’s happening:

  • a is a 1-row, 5-column data.table where each column holds an integer value (1, 2, 3, 4, 5).
  • When lapply(.SD, ...) runs, it iterates over each column of a. For each column, x is the single value in that column (since a only has one row).
  • b[,x] uses x as a column index for matrix b. R allows integer values to index matrix columns directly—so x=1 grabs the 1st column of b, x=2 grabs the 2nd, and so on.
  • Multiplying x (a single integer) by b[,x] (a 10-element vector) works because R recycles the single value to match the vector length, resulting in a valid 10-element vector per column. The data.table then assembles these vectors into new columns, so the code runs without errors.

The Failing Code: Non-Integer Values as Indices

Now the code that breaks:

a <- data.table(t(c(0.1, 0.2, 0.3, 0.4, 0.5)))
b <- matrix(data=1, nrow=10, ncol=5)
a[,lapply(.SD,function(x)(x*b[,x]))]

The problem boils down to matrix indexing rules:

  • a now holds non-integer (floating-point) values (0.1, 0.2, etc.) in its columns.
  • When b[,x] runs with x=0.1, R tries to use this floating-point number as a column index for b. But matrices only accept integer indices (or logical indices/column names) to reference columns—there’s no such thing as the "0.1th column" of a matrix.
  • R doesn’t automatically convert floating-point numbers to integers for matrix indexing here (unlike some other contexts), so this throws an invalid index error immediately.

Fixing the Failing Code

If your goal is to use the values in a as weights (not column indices), you’d want to do element-wise multiplication instead of indexing:

# Multiply each value in a by the entire matrix b (recycles rows of a to match b's rows)
a[, lapply(.SD, function(x) x * b)]

If you intended to use the values in a as column indices (but they’re stored as floats), convert them to integers first:

a <- data.table(t(c(0.1, 0.2, 0.3, 0.4, 0.5)))
a <- a[, lapply(.SD, as.integer)] # Convert columns to integers
b <- matrix(data=1, nrow=10, ncol=5)
a[, lapply(.SD, function(x) x * b[,x])] # Now runs successfully

内容的提问来源于stack exchange,提问作者Z117

火山引擎 最新活动