Hi Kevin, thank you so much!! I am a R beginner and did not know the "get" function.
I want to write a user defined function that I can replace part of R codes.
first, I ran the following code.
middle.pred.young <-PAMfunction(training=middle, met.status=middle$met,
test=young, ....)
correlation.middle <-corfunction(middle.pred.young)
(more R code with different functions to process the file
"middle.pred.young)
then, I replaced the word "middle" a few times to run the following codes;
old.pred.young <-PAMfunction(training=old, met.status=old$met, test=young,
....)
correlation.old <-corfunction(old.pred.young)
then more similar code I need to run;
all_age.pred.young <-PAMfunction(training=all_age, met.status=all_age$met,
test=young, ....)
correlation.allage <-corfunction(all_age.pred.young)
so, I only need to replace one word. I would like to copy SAS macro idea in R.
%macro predict;
&age..pred.young <-PAMfunction(training=&age, met.status=&age.$met,
test=young, ....);
correlation.&age <-corfunction(&age..pred.young);
.....;
%mend;
%predict(age="old")
%predict(age="middle")
%predict(age="all_age")
Can you do me a great favor to write R function to do the repetitive job?
Thank you,
Ding
2 answers
Hi Ding, this is the standard way of creating and then setting a function in R:
PAMfunction <- function(training, met.status=middle$met, test, ...)
{
correlation <- corfunction(get(paste(training, ".pred.", test, sep="")))
...
}
Then call the function
PAMfunction("old", old$met, "young")
In this situation, the following will be called corfunction(get("old.pred.yound")) The get() function will call a variable by its String name. If it's a file, as you seem to indicate, you will have to read it in with read.table() or read.csv()
If an answer was helpful you should upvote it, if the answer resolved your question you should mark it as accepted.

Welcome to biostars. Interesting guidelines for posting can be found in the following posts:
You didn't really specify whether you're interested in both results or just the last one.
The following function will return a list with both results (of PAMfunction and corfunction).
combinedFunction <- function(set_of_interest, test_set, ...){
dataset <- set_of_interest
pred_result <- PAMfunction(training = dataset,
met.status = dataset$met,
test = test_set, ....)
corr_result <- corfunction(pred_result)
return(list(pred_result, corr_result)
}
Use it so:
old <- combinedFunction(old, young)
middle <- combinedFunction(middle, young)
all <- combinedFunction(all_ages, young)
Hi Friederike, thank you so much!! yes, yesterday evening, I google searched about how to return multiple data frame from a function call and found that a R function can only return a list,not multiple individual data frames.
Log in to answer this question.
Hello ycding!
We believe that this post does not fit the main topic of this site.
Not a bioinformatics question.
For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.
If you disagree please tell us why in a reply below, we'll be happy to talk about it.
Cheers!
Hi Kevin,
Thank you so much!! yes, I am a R beginner and did not know the "get()" function.