So, I have a script which I would like to create a matrix or data.frame on the fly.
However, I do not know the dimensions of the matrix/frame going in. When I try to just create a blank matrix and adding values, I get the “subscript out of bounds” error,
Here is some of my code:
data <- read.csv("C:/3PP/data.txt", header=F)
parsed = matrix()
for (i in 1:nrow(data))
{
parsed[data[i,1],data[i,2]+1] = data[i,3]
}
How can I set this up such that this matrix can be generated on the fly, without having to specify the size in the beginning?
Thank you!
Given your problem, you know already the number of rows and columns. You can specify your matrix like:
The only other way to grow a matrix is by using
rbind()orcbind(), but that can get pretty slow with big matrices.