Monolite
Monolite

Reputation: 103

What is wrong with my R for loop? matrix access being reduced to vector

First time using R, I am trying to populate each row of a matrix of zeroes (mm) with a vector (wolo[[9]]) that changes in every iteration of the loop because it is a function of my dataframe.

for (i in 1:length(foo.squared)) { 
     wolo <- tegarch(googler, skew = FALSE, asym = FALSE)   
     mm[i,]<- wolo[[9]]
     googler <- googler[-(2659-i),]     
} 

Why do I get this error message?

Error in googler[-(2659 - i), ] : incorrect number of dimensions

The number of dimensions is correct if I type

 googler <- googler[-(2659-1),]

into the console it works fine.

My function does not change googler, so I suspect there is something more basic I am doing very wrong.

Upvotes: 0

Views: 109

Answers (1)

Cath
Cath

Reputation: 24074

I'm guessing googler only have one column so you can try to replace the last line in your loop by:

googler <- googler[-(2659-i), , drop=F]

to keep the structure of googler as data.frame or matrix (prevent it from being converted to a vector).

Upvotes: 2

Related Questions