Re: [R] by function does not separate output from function with mulliple parts
Às 00:22 de 25/10/2023, Sorkin, John escreveu: 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. Hello, What you are seeing is the prints and cats in the function DoReg, not its output. The code below works as expected. Also, you didn't load the package where function contrast() is found. library(contrast) # Define function to be run# DoReg <- function(x){ fit0 <- lm(as.numeric(HipFlex) ~ jSex,data=x) 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) #> mydata$StepType: First #> lm model parameter contrast #> #> Contrast S.E.LowerUppert df Pr(>|t|) #> 1 2.99114 1.956013 -1.05518 7.037461 1.53 23 0.1399 #> #> mydata$StepType: Second #> lm model parameter contrast #> #> Contrast S.E. LowerUpper t df Pr(>|t|) #> 1 -2.435 1.819421 -6.198759 1.328759 -1.34 23 0
Re: [R] by function does not separate output from function with, mulliple parts
Dear John, Printing inside the function is problematic. Your function itself does NOT print the labels. Just as a clarification: F = factor(rep(1:2, 2)) by(data.frame(V = 1:4, F = F), F, function(x) { print(x); return(NULL); } ) # V F # 1 1 1 # 3 3 1 # V F # 2 2 2 # 4 4 2 # F: 1 <- this is NOT printed inside the function # NULL # - # F: 2 # NULL ### Return Results by(data.frame(V = 1:4, F = F), F, function(x) { return(x); } ) # F: 1 # V F # 1 1 1 # 3 3 1 # -- # F: 2 # V F # 2 2 2 # 4 4 2 Maybe others on the list can offer further assistance. Sincerely, Leonard __ 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.
[R] by function does not separate output from function with mulliple parts
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.