[R] Passing arguments to with()

Duncan Murdoch murdoch at stats.uwo.ca
Wed Jul 8 17:26:06 CEST 2009


On 08/07/2009 10:01 AM, Tymek Wo?odz'ko wrote:
> Hi,
> 
> I've been wondering how to write a function that will produce results
> from multiple tests (eg. paired t-tests) for all or several variables
> in some data frame. I'd like it to do t-test for each variable ('x')
> in 'data' by 'y'. I'm stuck in here:
> 
> function(data,y) {
> for (x in names(data)) {
> 	with(data, t.test(x~y))
> }}
> 
> How to tell 'with' that 'x' and 'y' are names of columns in 'data'? Or
> pass similar arguments?

Don't use with.  Use t.test(data[[x]] ~ data[[y]]).

Duncan Murdoch

> 
> I probably understand the logic why this is not working, but still
> don't know how to make it work.
> 
> Thanks in advance for any help!
> Timo
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list