rm(list=ls())
library(dplyr)
library(table1)
dat <- iris %>%
mutate(.,color = rep(c("red","blue","green"), each = 50)) %>%
mutate(.,group = rep(c("big","small"), each = 75)) #在这里新增2列,后面会用到 (无中生有!,,???,,)
view(dat) #数据不大, 这里就view一下吧~ ?????
units(dat$Sepal.Length) <- "mm"
units(dat$Sepal.Width) <- "mm"
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width, # 以上均为变量
data=dat, # 你的数据
overall = "Total")
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width | Species, #Species为分组变量
data=dat, # 你的数据
overall = "Total")
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width | Species*color, #Species为分组变量,color为分层变量
data=dat, # 你的数据
overall = "Total")
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width | color*Species, #color为分组变量,Species为分层变量
data=dat, # 你的数据
overall = "Total")
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)))
}
stats <- function(x, ...) {
y <- unlist(x)
g <- factor(rep(1:length(x), times=sapply(x, length)))
if (is.numeric(y)) {
s <- t.test(y ~ g)$statistic
} else {
s <- chisq.test(table(y, g))$statistic
}
c("", sprintf("%.4f", s))
}
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width | group,
data=dat,
overall = F,
extra.col=list(`P-value`=pvalue, `statistic`=stats))
最后祝大家早日不卷!~
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。