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.

2
  • For your second question: look at stackoverflow.com/questions/1743698/r-eval-expression (you need to use parse) Commented Apr 8, 2014 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 " Commented Apr 9, 2014 at 0:58

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

5 Comments

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.
I have added some comment on my question, perhaps these will help u to understand my meaning.
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...
Yeah! It is a good way to solve it. Thank you so much! I does offer me a great help!
You're welcome. If it helped, you can accept my answer or vote it up as beeing useful. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.