Reputation: 2424
I'm having trouble loading my data set into a sparse matrix in R. I am using the Matrix package. The data I have is in the form x y value
. For example:
V1 V2 V3
1 2 .34
7 4 .56
4 5 .62
where I would want to do the equivalent of
myMatrix[1,2] = .34
myMatrix[7,4] = .56
myMatrix[4,5] = .62
in an automated fashion.
I want to do something like:
myMatrix = Matrix(nrow=numrows, ncol=numcols)
myMatrix[mydata[1:numrows, 1], mydata[1:numrows, 2]] <- mydata[1:numrows, 3]
but this makes my matrix a lgeMatrix when I need a numeric matrix.
I have also tried:
myMatrix = Matrix(nrow=numrows, ncol=numcols)
for(i in 1:numrows){
myMatrix[mydata[i, 1], mydata[i, 2]] <- mydata[i, 3]
}
Which creates the kind of matrix I want, but it takes way too long (more than 5 minutes). I know it works because when I stop it I check the first few values and they're correct, but the last values are NA. I'm working with a 7095 by 5896 matrix with 247158 values to enter so a for loop is out of the question, unless I'm just being impatient.
My question is: What is the preferred way to do this in R?
update:
I figured it out using sparseMatrix
instead:
myMatrix = sparseMatrix(i = mydata[1:numrows,1], j = mydata[1:numrows,2],
x = mydata[1:numrows,3])
didn't understand the sparseMatrix
usage in the other post
Upvotes: 3
Views: 1517
Reputation: 263332
Let's assume that is a dataframe named dat:
myMatrix = Matrix(0, nrow=10, ncol=10)
# Notice that you need to specify zero values to make it sparse.
myMatrix[cbind(dat$V1, dat$V2)] <- dat$V3
myMatrix
#--------------
10 x 10 sparse Matrix of class "dgCMatrix"
[1,] . 0.34 . . . . . . . .
[2,] . . . . . . . . . .
[3,] . . . . . . . . . .
[4,] . . . . 0.62 . . . . .
[5,] . . . . . . . . . .
[6,] . . . . . . . . . .
[7,] . . . 0.56 . . . . . .
[8,] . . . . . . . . . .
[9,] . . . . . . . . . .
[10,] . . . . . . . . . .
Upvotes: 6