R:将矩阵列号的向量扩展为填充这些列的矩阵



我在R中有两个向量,想基于它们生成一个新矩阵。

a=c(1,2,1,2,3)      # a[1] is 1: thus row 1, column 1 should be equal to...
b=c(10,20,30,40,50) # ...b[1], or 10.

我想生成矩阵"v",但没有我的"for"循环通过 v 列和我的乘法:

v = as.data.frame(matrix(0,nrow=length(a),ncol=length(unique(a))))
for(i in 1:ncol(v)) v[[i]][a==i] <- 1   # looping through columns of 'v'
v <- v*b

我相信有一种快速/优雅的方法可以在 R 中做到这一点。至少将"a"扩展到"v"的早期版本(在乘以"b"之前)。

多谢!

这是定义稀疏矩阵的一种方法。

Matrix::sparseMatrix(i = seq_along(a), j = a, x = b)
# Setup the problem:
set.seed(4242)
a <- sample(1:100, 1000000, replace = TRUE)
b <- sample(1:500, length(a), replace = TRUE)  
# Start the timer 
start.time <- proc.time()[3]
# Actual code
# We use a matrix instead of a data.frame
# The  number of columns matches the largest column index in vector "a"
v <- matrix(0,nrow=length(a), ncol= max(a))
v[cbind(seq_along(a), a)] <- b
# Show elapsed time
stop.time <- proc.time()[3]
cat("elapsed time is: ", stop.time - start.time, "seconds.n")
# For a million rows and a hundred columns, my prehistoric
# ... laptop says: elapsed time is:  2.597 seconds.
# these checks take much longer to run than the function itself
# Make sure the modified column in each row matches vector "a"
stopifnot(TRUE == all.equal(a, apply(v!=0, 1, which)))
# Make sure the modified value in each row equals vector "b"
stopifnot(TRUE == all.equal(rowSums(v), b))

最新更新