Colleagues,
I have written an R function (see fully annotated code below), with which I
want to process a dataframe within levels of the variable StepType. My program
works, it processes the data within levels of StepType, but the usual headers
that separate the output by levels of StepType are at the end of the listing
rather than being used as separators, i.e. I get
Regression results StepType First
Contrast results StepType First
Regression results StepType Second
Contrast results StepType Second
and only after the results are displayed do I get the usual separators:
mydata$StepType: First
NULL
------------------------------------------------------------------------------
mydata$StepType: Second
NULL
What I want to get is output that includes the separators i.e.,
mydata$StepType: First
Regression results StepType First
Contrast results StepType First
------------------------------------------------------------------------------
mydata$StepType: Second
Regression results StepType Second
Contrast results StepType Second
Can you help me get the separators included in the printed otput?
Thank you,
John
####################
# Create Dataframe #
####################
mydata <- structure(list(HipFlex = c(19.44, 4.44, 3.71, 1.95, 2.07, 1.55,
0.44, 0.23, 2.15, 0.41, 2.3, 0.22, 2.08, 4.61, 4.19, 5.65, 2.73,
1.46, 10.02, 7.41, 6.91, 5.28, 9.56, 2.46, 6, 3.85, 6.43, 3.73,
1.08, 1.43, 1.82, 2.22, 0.34, 5.11, 0.94, 0.98, 2.04, 1.73, 0.94,
18.41, 0.77, 2.31, 0.22, 1.06, 0.13, 0.36, 2.84, 5.2, 2.39, 2.99),
jSex = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L), levels = c("Male", "Female"), class =
"factor")),
row.names = c(NA, 50L), class = "data.frame")
mydata[,"StepType"] <- rep(c("First","Second"),25)
mydata
########################
# END Create Dataframe #
########################
############################
# Define function to be run#
############################
DoReg <- function(x){
fit0<-lm(as.numeric(HipFlex) ~ jSex,data=x)
print(summary(fit0))
cat("\nMale\n")
print(contrast(fit0,
list(jSex="Male")))
cat("\nFemale\n")
print(contrast(fit0,
list(jSex="Female")))
cat("\nDifference\n")
print(contrast(fit0,
a=list(jSex="Male"),
b=list(jSex="Female")))
}
################################
# END Define function to be run#
################################
#########################################
# Run function within levels of Steptype#
#########################################
by(mydata,mydata$StepType,DoReg)
#############################################
# END Run function within levels of Steptype#
#############################################
John David Sorkin M.D., Ph.D.
Professor of Medicine, University of Maryland School of Medicine;
Associate Director for Biostatistics and Informatics, Baltimore VA Medical
Center Geriatrics Research, Education, and Clinical Center;
PI Biostatistics and Informatics Core, University of Maryland School of
Medicine Claude D. Pepper Older Americans Independence Center;
Senior Statistician University of Maryland Center for Vascular Research;
Division of Gerontology and Paliative Care,
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
Cell phone 443-418-5382
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.