You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言将Publish包univariatetable输出列拆分为频数与百分比列

拆分Publish包univariatetable输出的合并列解决方案

首先安装并加载所需依赖包:

# 未安装可先执行 install.packages(c("dplyr", "stringr"))
library(dplyr)
library(stringr)

拆分逻辑:使用正则匹配括号作为分隔符拆分合并列,拆分结果直接作为新列写入原数据框,不会丢失原有Variable、LEVEL等变量:

# 拆分未接种组(Vaccinated=0)的合并列
split_col0 <- str_split_fixed(table1$`Vaccinated = 0 (n=34)`, "\\(|\\)", 3)
table1$`未接种_频数` <- as.numeric(split_col0[,1])
table1$`未接种_百分比` <- split_col0[,2]

# 拆分已接种组(Vaccinated=1)的合并列
split_col1 <- str_split_fixed(table1$`Vaccinated = 1 (n=52)`, "\\(|\\)", 3)
table1$`已接种_频数` <- as.numeric(split_col1[,1])
table1$`已接种_百分比` <- split_col1[,2]

# 可选操作:删除原始合并列
table1 <- select(table1, -c(`Vaccinated = 0 (n=34)`, `Vaccinated = 1 (n=52)`))

如果需要将百分比列转为数值型用于后续计算,可补充执行如下代码:

table1$`未接种_百分比` <- as.numeric(str_remove(table1$`未接种_百分比`, "%"))
table1$`已接种_百分比` <- as.numeric(str_remove(table1$`已接种_百分比`, "%"))

该方案比按空格拆分更稳定,不会因数值位数差异导致的空格数量变化出现拆分错误。

内容的提问来源于stack exchange,提问作者Mari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 08:06:04