0

I want to run a for loop that does a vector-matrix operation and returns a vector suffixed by the iteration number. E.g: If I have a 5 by 5 matrix , I want to take each column of the matrix at a time (at each iteration of the for loop) and work on a bunch of operations and at the end of it get a vector that is labelled as v_i where i refers to the column index and also the iteration number of the loop. I understand that this can be achieved in a for loop but I am not sure how to label the variable at each iteration.

For instance if I had to do this in SAS, I would have used v&i and put in a macro and run it. But not sure what is the R equivalent of this iterative labelling of variables.

Would really appreciate any help on this. I have a homework due next week and am in real crunch time. Thanks!

sharmi
  • 5
  • 1

2 Answers2

0

It's not good programming practice to create objects out of a loop like that. It's better to put them in one object (say, a list) and populate the list from inside the loop. You can even create the list before running the loop.

I'm not sure if I understood correctly, but here's a very simple example that calculates the mean per column. To me, your question didn't actually reveal what you try to accomplish.

mat <- matrix(1:25, ncol=5)

lst <- as.list(numeric(ncol(mat)))
names(lst) <- sapply(1:ncol(mat), function(x) paste("v_",x,sep=""))

myfun <- mean

for(i in 1:ncol(mat)){
  lst[[i]] <- myfun(mat[,i])
}

Cheers!

SimonG
  • 4,701
  • 3
  • 20
  • 31
0

You'll be better served if you spend a bit of time learning how R works and how it is different from SAS. Lucky for you, there are resources dedicated to such a learning curve - see here.

In this case, you don't need to use a loop at all. I also doubt that you actually want a list output as SimonG suggests, rather a simple vector. Here's an example:

mat <- matrix(1:25, ncol=5)
#Give the matrix some names
colnames(mat) <- paste0("col_", 1:5)
#compute the column means
colMeans(mat)
---

col_1 col_2 col_3 col_4 col_5 
    3     8    13    18    23 
Chase
  • 67,710
  • 18
  • 144
  • 161
  • This works for functions like `colMeans` that are predefined. For other functions, the likes of `apply` would be needed, which would be very close to looping again. In this case I find `for` easier to handle for beginners. The question didn't specify if it's actually the mean that is needed. – SimonG Oct 04 '14 at 22:30
  • Thanks Chase for your response! My bad that I did not specify the actual operation that I needed to do. To that extent, I think SimonG's solution actually works for my purpose. Appreciate your inputs as well! Thanks! – sharmi Oct 04 '14 at 22:49
  • @SimonG - fair enough, though I still think a simple vector output would be more than sufficient, as long as the OP isn't estimating a set of models / generating some other complex output that requires a list structure...it wouldn't surprise me if there's a "how do I collapse this list" question coming soon :) – Chase Oct 04 '14 at 23:51
  • @sharmi - no worries. In the future, try and ask your questions so that they follow [these guidelines](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It will ensure you get more useful answers. – Chase Oct 04 '14 at 23:53