当前位置:网站首页>🧐 table1 | 一秒搞定你的三线表
🧐 table1 | 一秒搞定你的三线表
2022-07-28 23:31:00 【jamesbang】
1. 需要的包
rm(list=ls())
library(dplyr)
library(table1)
2. 示例数据
本次使用的是大名鼎鼎的iris,这里你可以替换成你的数据
dat <- iris %>%
mutate(.,color = rep(c("red","blue","green"), each = 50)) %>%
mutate(.,group = rep(c("big","small"), each = 75)) #在这里新增2列,后面会用到 (无中生有!,,Ծ‸Ծ,,)
view(dat) #数据不大, 这里就view一下吧~ ๐•ᴗ•๐

3. 给变量添加单位(可选)
这里以变量Sepal.Length和Sepal.Width为例,添加单位,将在三线表中显示。
units(dat$Sepal.Length) <- "mm"
units(dat$Sepal.Width) <- "mm"
4. 开始绘制三线表
4.1 初步绘制
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width, # 以上均为变量
data=dat, # 你的数据
overall = "Total")

4.2 分组绘制 (单变量分组)
以Species为分组变量进行统计
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width | Species, #Species为分组变量
data=dat, # 你的数据
overall = "Total")

4.3 分组绘制 (双变量分组)
由于iris没有第二个分组变量,我们在之前新增了一列color作为分层变量
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width | Species*color, #Species为分组变量,color为分层变量
data=dat, # 你的数据
overall = "Total")

Note: 这里大家要注意var*var前后顺序,意义是不一样的,我们调换一下Species和color的顺序
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width | color*Species, #color为分组变量,Species为分层变量
data=dat, # 你的数据
overall = "Total")

5. 填加统计值
5.1 自定义函数计算统计值
这个包美中不足的地方,没有统计值啊~,大家复制这段代码添加统计值
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))
}
5.2 绘制带统计值的三线表
table1(~ Sepal.Length +
Sepal.Width +
Petal.Length +
Petal.Width | group,
data=dat,
overall = F,
extra.col=list(`P-value`=pvalue, `statistic`=stats))


点个在看吧各位~ *.ɴɪᴄᴇ ᴅᴀʏ

边栏推荐
- Main thread and daemon thread
- Longest ascending subsequence
- Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
- [untitled]
- NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
- Techo hub Fuzhou Station dry goods attack | talk with developers about new industrial intelligence technology
- 【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
- Breadth first search (BFS) and its matlab code
- Requestvideoframecallback() simple instance
- Kwai focuses on regulating the number maintenance behavior in the ways of handling and manuscript washing, and how to purify the content ecology on the we media platform
猜你喜欢

将行内元素转换为块元素的方法

Copy the table in word to wechat as a picture and send it

Error reporting: when the browser clicks the modify add button, there is no response and no error reporting. Solution

将Word中的表格以图片形式复制到微信发送

Selenium docking agent and selenium wire access developer tool network

Educational Codeforces Round 132 (Rated for Div. 2)【A~C】

Five interesting magic commands in jupyter notebook

iNFTnews | 元宇宙购物体验将成为吸引消费者的一大利器

Solutions such as failed plug-in installation and slow speed of linking remote server under vscode

"Food alliance ordering system"
随机推荐
Outlier detection and open set identification (1)
Main thread and daemon thread
直流无刷电机控制器(换电机霍尔收费多少)
第二轮1000个Okaleido Tiger,再次登录Binance NFT 1小时售罄
Data warehouse construction - DWT floor
Requestvideoframecallback() simple instance
[network security] complete the blacklist and whitelist functions of server firewall through iptables and ipset
Soft test --- database (4) SQL statement
Api 接口优化的那些技巧
SQL server only has database files and no log files. The solution to the 1813 error in restoring data times
andriod6.0低功耗模式(关闭wifi、蓝牙、gps、屏幕亮度等)
CUDA related
Have you seen the management area decoupling architecture? Can help customers solve big problems
Station B "crashed" from beginning to end 2021.07.13 we collapsed like this (Reprint)
Selenium wire obtains Baidu Index
Application and principle of distributed current limiting redistribution rratelimiter
SurfaceControl和SurfaceFlinger通信
时间序列数据的预处理方法总结
【愚公系列】2022年07月 Go教学课程 020-Go容器之数组
Xinchi technology released the latest flagship product of G9 series, equipped with six A55 cores with 1.8GHz dominant frequency