I'm using the example given here to try to generate a column with p-values, however, my code didn't work... and the original example from the package manual doesn't seem to generate a third column either. I've been trying to figure out the issue and I'm at a loss. Anyone have any ideas?
Source: https://cran.r-project.org/web/packages/table1/vignettes/table1-examples.html
library(table1)
library(MatchIt)
data(lalonde)
lalonde$treat <- factor(lalonde$treat, levels=c(0, 1), labels=c("Control", "Treatment"))
lalonde$married <- as.logical(lalonde$married == 1)
lalonde$nodegree <- as.logical(lalonde$nodegree == 1)
lalonde$race <- factor(lalonde$race, levels=c("white", "black", "hispan"),
labels=c("White", "Black", "Hispanic"))
pvalue <- function(x, ...) {
# Construct vectors of data y, and groups (strata) g
y <- unlist(x)
g <- factor(rep(1:length(x), times=sapply(x, length)))
if (is.numeric(y)) {
# For numeric variables, perform a standard 2-sample t-test
p <- t.test(y ~ g)$p.value
} else {
# For categorical variables, perform a chi-squared test of independence
p <- chisq.test(table(y, g))$p.value
}
# Format the p-value, using an HTML entity for the less-than sign.
# The initial empty string places the output on the line below the variable label.
c("", sub("<", "<", format.pval(p, digits=3, eps=0.001)))
}
table1(~ age + race + married + nodegree + re74 + re75 + re78 | treat,
data=lalonde, overall=F, extra.col=list(`P-value`=pvalue))
Solved problem:
(1) Uninstall Table1 downloaded from CRAN
(1.1) Make sure RTools is downloaded and installed https://cran.r-project.org/bin/windows/Rtools/ at first my PC was not recognizing the program, but after several tries I was able to execute it
(2) Reinstall version from github remotes::install_github("benjaminrich/table1")
(2.1) Make sure other packages such as xfun are also updated to the latest version
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.