Reputation: 83
How can I improve the speed of following codes?
for (i in 1:nrow(training)){
score[training[i,1],training[i,2],training[i,4]] = training[i,3]
}
Training
is a matrix with four columns. I just want to build an array which the value is training[i,3]
according the formula above.
Thanks!
Upvotes: 0
Views: 58
Reputation: 89097
You can index using a matrix. Here is the relevant part of [
's documentation:
A third form of indexing is via a numeric matrix with the one column for each dimension: each row of the index matrix then selects a single element of the array, and the result is a vector.
So in your case, the for
loop can be replaced with:
score[training[, c(1, 2, 4)]] <- training[, 3]
Upvotes: 6