R_Matrix_loop: Error in loop through columns

43 views Asked by At

I have two data frames 'obs' and 'sim' each with 25 columns. I want to do the following operation using for- loop

for(i in 2:25) {
obs<-obs[,i]
sim<-sim[,i]
plot(sim,obs)
}

But it gives an error 'Error in obs[, i] : incorrect number of dimensions'. I am sure both data frames are Matrix. And when I do without the loop it works (eg: obs[,2], obs[,3], and so on). I don't know what's wrong with the loop. Anyone to help?

2

There are 2 answers

0
stanekam On BEST ANSWER

In that loop you are changing the size of obs on every single iteration to a one dimensional object at this line:

obs<-obs[,i]

So now obs is just the 'ith' column of obs and then on your second iteration your loop barfs.

0
rm167 On

Thank you very much @iShouldUseAName. Renaming works fine.

for(i in 2:25) {
obsf<-obs[,i]
simf<-sim[,i]
plot(simf,obsf)
}

It's a silly mistake. I should have figured it out myself. ;)