Reputation: 703
I am generating a sparse vector length >50,000. I am producing it in a for loop. I wonder if there is an efficient way of storing the zeros?
Basically the code looks like
score = c()
for (i in 1:length(someList)) {
score[i] = getScore(input[i], other_inputs)
if (score[i] == numeric(0))
score[i] = 0 ###I would want to do something about the zeros
}
Upvotes: 2
Views: 903
Reputation: 121608
This code will not work. You should preallocate score vector size before looping. Preallocating also will create a vector with zeros. So, no need to assign zeros values, you can only assign numeric results from getScore
function.
N <- length(someList) ## create a vector with zeros
score = vector('numeric',N)
for (i in 1:N) {
ss <- getScore(input[i], other_inputs)
if (length(ss)!=0)
score[i] <- ss
}
Upvotes: 1