1

There are several questions:

1 In an R function, "return" only can output one plot or value. But now, I want the function output every plot or vector I require, how could I achieve that. Which code should I use.

2 I have a series of variables : Game1~Game10 and I built up a do loop to analysis each of them where I represented their name as
"paste("Game",i, sep="")",
But it is characters, and I cannot do it like a variable like
"sort(eval(paste("Game",i, sep="")))"
is fail. How could I make R recognize the characters series as a variable name.

Chen
  • 117
  • 1
  • 1
  • 9
  • For your second question: look at http://stackoverflow.com/questions/1743698/r-eval-expression (you need to use `parse`) – Jealie Apr 08 '14 at 22:03
  • @Jealie Thank u so much! But I need some further help. I tried it like : eval(parse(paste("Game",i,sep=""))) There is Game1 in my workfile and it said :"cannot open file 'Game1': No such file or directory " – Chen Apr 09 '14 at 00:58

1 Answers1

1

to return more than one value from a function, use a data structure, that can store more values and return it, e.g. a vector, list or a dataframe

...
vector_1 <- 1:10
vector_2 <- 11:20
return( list(vec_1=vector_1, vec_2=vector_2) )

to output more than one plot, simply use a loop within the function e.g.

for(i in 5:10) plot(1:i)

Your second question is not clear to me. What are you trying to do?

Christian Borck
  • 1,812
  • 1
  • 13
  • 19
  • Like a1<-c(1,2) and How could I represent the number of variable a1 into something like "a"&i. In my loops, I want to represent variable regularly. But R just reads them as characters or strings instead of variable. How could I tell R clearly which is string and which is variable. – Chen Apr 09 '14 at 00:29
  • I have added some comment on my question, perhaps these will help u to understand my meaning. – Chen Apr 09 '14 at 00:59
  • I think you should try a simpler approach. Instead of pasting togehter numbers and variable names why don't you use a list? E.g.: `a <- list()` and then `for(i in 1:10) a[[i]] <- c(1,2)` This gives you the list "a" with a vector element for every loop run... – Christian Borck Apr 09 '14 at 07:01
  • Yeah! It is a good way to solve it. Thank you so much! I does offer me a great help! – Chen Apr 09 '14 at 07:54
  • You're welcome. If it helped, you can accept my answer or vote it up as beeing useful. Thanks – Christian Borck Apr 09 '14 at 08:00