[R] Function to tabulate t-test (1) How can I get dependent variable name (2) How can I make data frame know to function

Bert Gunter gunter.berton at gene.com
Mon Jan 7 16:37:28 CET 2013


John:

R2.12 ??? Time to update.

1. ?t.test and note the last entry in the Value section.

2.  ?with

-- Bert

On Mon, Jan 7, 2013 at 6:44 AM, John Sorkin <JSorkin at grecc.umaryland.edu> wrote:
> Windows 7
> R 2.12.1
> I am trying to write a function (see sample code below) that will take the output of a t-test and produce results suitable for a table.
> I have two questions
> (1) You will note that the name of the outcome variable, which is "value" in the input is replaced by the string "outcome by class" in the data frame produced by my function. How can I make my function put the name of the variable being analyzed,, i.e "value" in the output data frame
> (2) How can I pass the entire input data to the function so my call to the function will not have to be in its current ugly form, i.e.
> Table1(data$value,data$sex)
> and instead could just be
> Table1(value,sex,data=data)
>
>
> x <- data.frame(value=rnorm(20)  ,sex=rep("Male",  20))
> y <- data.frame(value=rnorm(20,4),sex=rep("Female",20))
> data <- rbind(x,y)
> temp <- t.test(value~sex,data=data)
> temp
> v<-data.frame(dep=temp$data.name,
>            female=temp$estimate[1],male=temp$estimate[2],
>            p=temp$p.value,
>            CILow=temp$conf.int[1],CIHigh=temp$conf.int[2])
> row.names(v) <- NULL
>
>
>
> Table1 <- function(outcome,class) {
> temp <- t.test(outcome~ class)
> mydf <- data.frame(dep=temp$data.name,
>              female=temp$estimate[1],male=temp$estimate[2],
>              p=temp$p.value,
>              CILow=temp$conf.int[1],CIHigh=temp$conf.int[2])
> row.names(mydf) <- NULL
> mydf}
> Table1(data$value,data$sex)
>
>
>
> Thank you,
> John
>
>
>
> John David Sorkin M.D., Ph.D.
> Chief, Biostatistics and Informatics
> University of Maryland School of Medicine Division of Gerontology
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
> (Phone) 410-605-7119
> (Fax) 410-605-7913 (Please call phone number above prior to faxing)
> Confidentiality Statement:
> This email message, including any attachments, is for ...{{dropped:27}}




More information about the R-help mailing list