当前位置:网站首页>🧐 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))


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

边栏推荐
- Jupyter notebook中5个有趣的魔法命令
- Error reporting: the network preview shows {xxx:['this field is required']}
- NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
- [development tutorial 11] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - explanation of the function code of the whole machine
- [development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver
- UE4 调试常用的打印信息方法
- How to solve the problem that the Oracle instance cannot be started
- Selenium wire obtains Baidu Index
- Flash and seven cattle cloud upload pictures
- I was asked several questions about string in the interview. Can you answer them?
猜你喜欢

自制 | 纯手工自制一个16位RISC架构CPU

靠云业务独撑收入增长大梁,微软仍然被高估?

NPM run serve stuck at 40%

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(7)中期检查报告
![Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]](/img/e3/496247afdb3ea5b9a9cdb8afb0d41b.png)
Cloud function realizes website automatic check-in configuration details [web function /nodejs/cookie]

【愚公系列】2022年07月 Go教学课程 020-Go容器之数组

JWT token related configuration (global configuration identity authentication rewrites authenticate method)

Api 接口优化的那些技巧

Huawei releases harmonyos 3.0, taking another step towards "Internet of all things"

Outlier detection and open set identification (1)
随机推荐
Application and principle of distributed current limiting redistribution rratelimiter
Protective copy & stateless
Flask sends verification code in combination with Ronglian cloud
I was asked several questions about string in the interview. Can you answer them?
Flash and seven cattle cloud upload pictures
Selenium wire obtains Baidu Index
[network security] complete the blacklist and whitelist functions of server firewall through iptables and ipset
Some operations of Ubuntu remote server configuration database (unable to locate package MySQL server, steps of installing mysql, unable to enter password when logging in MySQL)
云函数实现网站自动化签到配置详解【Web函数/Nodejs/cookie】
SAP vl02n delivery note posting function WS_ DELIVERY_ UPDATE
Anti shake and throttling
时间序列数据的预处理方法总结
day8
[development tutorial 10] crazy shell · open source Bluetooth heart rate waterproof sports Bracelet - Bluetooth ble transceiver
Common measurement matrix and matlab code of compressed sensing
NFTScan 与 NFTPlay 在 NFT 数据领域达成战略合作
Depth first search (DFS) and its matlab code
day8
返回*this的成员函数
Solutions such as failed plug-in installation and slow speed of linking remote server under vscode