문제

I have a sparse matrix represented as

> (f <- data.frame(row=c(1,2,3,1,2,1,2,3,4,1,1,2),value=1:12))
   row value
1    1     1
2    2     2
3    3     3
4    1     4
5    2     5
6    1     6
7    2     7
8    3     8
9    4     9
10   1    10
11   1    11
12   2    12

Here the first column is always present (in fact, the first few are present, the rest are not).

I want to get the data into the matrix format:

> t(matrix(c(1,2,3,NA,4,5,NA,NA,6,7,8,9,10,NA,NA,NA,11,12,NA,NA),nrow=4,ncol=5))
     [,1] [,2] [,3] [,4]
[1,]    1    2    3   NA
[2,]    4    5   NA   NA
[3,]    6    7    8    9
[4,]   10   NA   NA   NA
[5,]   11   12   NA   NA

Here is what seems to be working:

> library(Matrix)
> as.matrix(sparseMatrix(i = cumsum(f[[1]] == 1), j=f[[1]], x=f[[2]]))
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    0
[2,]    4    5    0    0
[3,]    6    7    8    9
[4,]   10    0    0    0
[5,]   11   12    0    0

Except that I have to replace 0 with NA myself.

Is there a better solution?

도움이 되었습니까?

해결책

You can do everything with base functions. The trick is to use indexing by a 2-col (row and col indices) matrix:

j <- f$row
i <- cumsum(j == 1)
x <- f$value
m <- matrix(NA, max(i), max(j))
m[cbind(i, j)] <- x
m

Whether it is better or not than using the Matrix package is subjective. Overkill in my opinion if you are not doing anything else with it. Also if your data had 0 in the f$value column, they would end up being converted as NA if you are not too careful.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top