Hi,
I am new to R.
I am trying to create an R function to do a SAS proc means/summary
proc.means ( data=bsebal;
class team year;
var ab h;
output out=BseBalAvg mean=;
run;)
I have a solution if I quote the the argument. The working code to produce
BseBalAvg
is very elegant.
normalize <- melt(bsebal, id=c("team", "year"))
#
normalize data
transpose <- cast(normalize, team + year ~ variable ,mean) # team year h
ab (means)
Here is the problem
In SAS we have the option parmbuff which puts all the 'macro arguments'
text
into one string
ie
%macro procmeans(text)/parmbuff;
%put &text;
%mend procmeans;
%procmeans(This is a sentence);
result
This is a sentence
Here is my R code
# This works
proc.means <- function(....) {
sapply(match.call()[-1],deparse)
}
proc.means(thisisasentence)
Result
....
"thisisasentence"
Note sapply allows for multiple arguments
and is not needed but is more robust.
# However this does not work
proc.means(this is a sentence)
unexpected symbol in "proc means(this is)
It appears that the second space causes the error
I have had some luck using formulas
# This works in spite of the spaces
proc.means <- function(formula) {
parmbuff <- deparse(substitute(formula))
print(parmbuff)
}
proc.means(team + year + variable)
# this does not work - same issue as above
proc.means(team year variable)
unexpected symbol in "proc means(team year)
--
View this message in context:
http://r.789695.n4.nabble.com/SAS-Proc-summary-means-as-a-R-function-tp2286888p2286888.html
Sent from the R help mailing list archive at Nabble.com.
On 07/12/2010 07:16 PM, Roger Deangelis wrote:> > Hi, > > I am new to R. > > I am trying to create an R function to do a SAS proc means/summary > > proc.means ( data=bsebal; > class team year; > var ab h; > output out=BseBalAvg mean=; > run;) >So you're actually trying to have R generate the SAS code? R is not, in general, a macro language, and attempts to use it as such are fighting against the current. Since you're writing your own function, you can make it accept as many arguments as you want, even an arbitrary number. For instance, test <- data.frame(a = rnorm(100), b = rnorm(100, 10), c = rnorm(100, 20)) summarize <- function(...) { dots <- list(...) lapply(dots, summary) } summarize(test$a, test$b, test$c) Is that what you'd like?