I've just realized that you're swapping female and male in the creation
of the results data frame.
It should be
Table2 <- function(formula, data) {
dname <- rownames(attr(terms(formula), "factors"))[1]
temp <- t.test(formula, data)
mydf <- data.frame(dep=temp$data.name,
female=temp$estimate[2], male=temp$estimate[1],
p=temp$p.value,
CILow=temp$conf.int[1],CIHigh=temp$conf.int[2])
row.names(mydf) <- NULL
mydf
}
Rui Barradas
Em 07-01-2013 15:39, Rui Barradas escreveu:
Hello,
A simple change to your function partly answers to both questions.
Table2 <- function(formula, data) {
temp <- t.test(formula, data)
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
}
Table2(value ~ sex, data)
If you want the outcome variable to be called just "value", inside the
function use
dname <- rownames(attr(terms(formula), "factors"))[1]
mydf <- data.frame(dep=dname, [...etc...]
Hope this helps,
Rui Barradas
Em 07-01-2013 14:44, John Sorkin escreveu:
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:13}}
______________________________________________
[email protected] 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.
______________________________________________
[email protected] 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.