[R] Accesing data frame members from within functions

2008-05-02 Thread David Schwab
I am writing a simple R program to execute a t-test repeatedly on data contained in a data frame. My data looks like this: Category Value1 Value2 1 .5 .8 1 .3

Re: [R] Accesing data frame members from within functions

2008-05-02 Thread Henrique Dallazuanna
Try: foo - function(data, ...) { res - unlist(lapply(split(data, data$Category), function(.x)t.test(.x$Value1, .x$Value2)$p.value)) test - merge(data, as.data.frame(res), by.x=Category, by.y = 0) return(test) } x - data.frame(Category = rep(1:15, each = 10), Value1 = rnorm(150), Value2 =

Re: [R] Accesing data frame members from within functions

2008-05-02 Thread Jorge Ivan Velez
Hi David, Try this: # Data set set.seed(123) Category=as.factor(rep(1:15,each=10)) Value1 = rnorm(150) Value2= rnorm(150) yourdata=data.frame(Category,Value1,Value2) # Global function TTEST=function(mydata){ # Internal function tt=function(x,y) t.test(x,y)$p.value # p-values for(i in