[R] Obtaining p values from t-test run with a by function

Sorkin, John j@ork|n @end|ng |rom @om@um@ry|@nd@edu
Fri Jun 12 23:34:44 CEST 2020

I am trying to retrieve the p values produced by a Student's t-test run using a by function, but can not do so. I can easily get the p value when I run s Student's t-test without a by function. What is the secret to obtaining results returned from a function run within a by function.

An annotated repeatable example (including data) can be found below.
Thank you,

# Test data
mydata <- structure(list(Group = structure(c(2L, 1L, 2L, 2L, 1L, 2L, 1L,
           2L, 2L, 1L, 2L, 1L, 2L), .Label = c("EPA", "P"), class = "factor"),
           WtBaseline = c(76.6, 73.8, 77.6, 91.7, 110.3, 121.7, 82.1,
           82.8, 119, 88.4, 75.7, 71.4, 72.1)), class = "data.frame", row.names = c(NA,-13L))

cat("This is what mydata looks like\n")

result <- by(mydata$WtBaseline,mydata$Group,t.test)
cat("Student's t-test run using by command\n")
cat("Result has results for both groups, EPA and P\n")

cat("I can isolate the collective results for group EPA\n")
cat("I can isolate the collective results for group P\n")

cat("I cant get the p-values for the gruops")

cat("When run without by function, one can get the p value\n")
xxx <- t.test(WtBaseline~Group,data=mydata)
cat("t-test run without by fundtion\n")
cat("p value isolated from t-test run without by function\n")

John David Sorkin M.D., Ph.D.
Professor of Medicine
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine
Baltimore VA Medical Center
10 North Greene Street
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)

	[[alternative HTML version deleted]]

More information about the R-help mailing list