Blessed Geek
Blessed Geek

Reputation: 21654

Appending to array within a loop

I am a SAS programmer learning R.

I have a matrix receivables read from a csv file.

I wish to read the value in the "transit" column, if the value of "id" column of a row is >= 1100000000.

I did this (loop 1):

x = vector()
for (i in 1:length(receivables[,"transit"])){
  if(receivables[i,"id"] >= 1100000000){
     append(x, receivables[i,"transit"]);
  }
}

But, it does not work because after running the loop x is still empty.

>x
logical(0)

However, I was able to accomplish my task with (loop 2):

k=0
x=vector()
for (i in 1:length(receivables[,"transit"])){
  if(receivables[i,"id"] >= 1100000000){
     k=k+1
     x[k] = receivables[i,"transit"]
  }
}

Or, with (loop 3):

x = vector()
for (i in 1:length(receivables[,"transit"])){
  if(receivables[i,"id"] >= 1100000000){
     x <- append(x, receivables[i,"transit"]);
  }
}

Why didn't the append function work in the loop as it would in command line?

Actually, to teach me how to fish, what is the attitude/beatitude one must bear in mind when operating functions in a loop as opposed to operating them in command line.

Which is more efficient? Loop 2 or loop 3?

Upvotes: 0

Views: 91

Answers (1)

joran
joran

Reputation: 173577

Ok, a few things.

append didn't work in your first attempt because you did not assign the result to anything. In general, R does not work "in place". It is more of a functional language, which means that changes must always be assigned to something. (There are exceptions, but trying to bend this rule too early will get you in trouble.)

A bigger point is that "growing" objects in R is a big no-no. You will quickly start to wonder why anyone could possible use R, because growing objects like this will quickly become very, very slow.

Instead, learn to use vectorized operations, like:

receivables[receivables[,"id"] >= 1100000000,"transit"]

Upvotes: 4

Related Questions