2

I'm learning R and trying to use it for a statistical analysis at the same time. Here, I am in the first part of the work: I am writing matrices and doing some simple things with them, in order to work later with these.

punti<-c(0,1,2,4)
t1<-matrix(c(-8,36,-8,-20,51,-17,-17,-17,57,-19,-19,-19,35,-8,-19,-8,0,0,0,0,-20,-20,-20,60,
         -8,-8,-28,44,-8,-8,39,-23,-8,-19,35,-8,57,-8,-41,-8,-8,55,-8,-39,-8,-8,41,-25,
         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),ncol=4,byrow=T)
colnames(t1) <- c("20","1","28","19")
r1<-matrix(c(12,1,19,9,20,20,11,20,20,11,20,28,0,0,0,12,19,19,20,19,28,15,28,19,11,28,1,
         33,20,28,31,1,19,17,28,19,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA),ncol=3,byrow=T)
pt1<-rbind(sort(colSums(t1)),sort(punti))
colnames(r1)<-c("Valore","Vincitore","Perdente")
r1<-as.data.frame(r1)

But I have more matrices t_ and r_ so I would like to run a for-loop like:

for (i in 1:150)
{
  pt[i]<-rbind(sort(colSums(t[i])),sort(punti))
  colnames(r[i])<-c("Valore","Vincitore","Perdente")
  r[i]<-as.data.frame(r[i])
}

This one just won't work because r_, t_ and pt_ are strings, but you get both the idea and that I would not like to copy-paste these three lines and manually edit the [i] 150 times. Is there a way to do it?

  • 2
    Welcome to StackOverflow! Please remember to make your examples *reproducible* if at all possible. Perhaps you could `dput` the `head()` of your data objects. – Hack-R Jul 19 '17 at 15:23
  • 2
    As a follow up on @Hack-R comment: Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – M-- Jul 19 '17 at 15:23
  • Thank you. I will add what needed. I was trying to keep it the most concise as possibile, since that was not the problem, but yes, you're right. – Davide Mastrullo Jul 19 '17 at 15:38
  • You can set column names at the very end, it doesn't need to be done 150 times in the `for` loop – Mako212 Jul 19 '17 at 16:26
  • I will have `r1`, `r2`, `r3`,..., `r150`. I think I need `colnames` in the for-loop – Davide Mastrullo Jul 19 '17 at 16:58
  • `t.names <- paste("pt", 1:150, sep=""); assign(t.names[1], "Whatever you want") ; assign(t.names[2], 3.14159)` etc. – klumbard Jul 19 '17 at 17:13

1 Answers1

0

personally i don't advise dynamically and automatically creating lots of variables in the global environment, and would advise you to think about how you can accomplish your goals without such an approach. with that said, if you feel you really need to dynamically create all these variables, you may benefit from the assign function.

it could work like so:

for (i in 1:150)
{
assign(paste0('p',i),rbind(sort(colSums(t[i])),sort(punti))) 
}

the first argument in the assign function is the formula for the variable name and how it is created; the second argument is what you wish to assign to the variable being created.

simitpatel
  • 641
  • 5
  • 9
  • I slightly modified your suggestion, because `colSums(t[i])` doesn't work `(Error in t[i] : object of type 'closure' is not subsettable)`. I tried `for (i in 1:150) { assign(paste0('p',i),rbind(sort(colSums(paste0('t',i))),sort‌​(punti))) }` but it doesn't work either. It reports `Error in colSums(paste0("t", i)) : 'x' must be at least a two-dimensional array` (translated from Italian, it may not be the perfect translation) – Davide Mastrullo Jul 20 '17 at 08:04