0

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?

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Blessed Geek
  • 21,058
  • 23
  • 106
  • 176
  • 1
    Why not just `receivables[receivables[,"id"] >= 1100000000,"transit"]`? – joran Nov 25 '13 at 17:33
  • Thanks Joran, I also wanted to ask for a better way. However, I still want to know the reason in difference between operating functions in a loop vs on cmd line. Thx. – Blessed Geek Nov 25 '13 at 17:35

1 Answers1

4

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"]
joran
  • 169,992
  • 32
  • 429
  • 468
  • +1! I would also add that it is preferable to use [`xxpply`](http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega) family to do a loop to avoid side effect.. – agstudy Nov 25 '13 at 17:40