我目前正在进行一项研究,旨在比较接受特定类型脑部手术的患者的术后并发症。在我们的一项分析中,我们希望比较患者(使用 Createtableone 功能)在脑部手术后经历的症状。
我们面临的问题是我们收集了不同类别变量中的症状。每个患者最多可能有四种不同的症状,但有些患者的症状少于四种。我们将症状归类为 FND(局灶性神经功能缺损),产生四个变量:FNDtype、FNDtype2、FNDtype3 和 FNDtype4。现在,我想将这些变量合并为一个,以便我可以进行描述性分析,显示每个分层组的每种症状的频率。
以下是 Rscript 的相关部分:
# Postcraniectomia Infection
data$postcraniotomy_infection <- factor(data$`Postcraniotomy infection (y=1, n=0)`, levels = c(0,1), labels = c("no", "yes"))
# FND Type
data$FNDtype <- factor(data$`Type of FND (1)`, levels = c(0,1,2,3,4,5,6,7,8,9), labels = c("none", "hemiparesis", "hypoesthesia", "aphasia", "visual field defects", "coordiantion disturbance", "cranial nerve dysfunction", "neglect","dysarthria","other"))
# FND Type 2
data$FNDtype2 <- factor(data$`Type of FND (2)`, levels = c(0,1,2,3,4,5,6,7,8,9), labels = c("none", "hemiparesis", "hypoesthesia", "aphasia", "visual field defects", "coordiantion disturbance", "cranial nerve dysfunction", "neglect","dysarthria","other"))
# FND Type 3
data$FNDtype3 <- factor(data$`Type of FND (3)`, levels = c(0,1,2,3,4,5,6,7,8,9), labels = c("none", "hemiparesis", "hypoesthesia", "aphasia", "visual field defects", "coordiantion disturbance", "cranial nerve dysfunction", "neglect","dysarthria","other"))
# FND Type 4
data$FNDtype4 <- factor(data$`Type of FND (4)`, levels = c(0,1,2,3,4,5,6,7,8,9), labels = c("none", "hemiparesis", "hypoesthesia", "aphasia", "visual field defects", "coordiantion disturbance", "cranial nerve dysfunction", "neglect","dysarthria","other"))
# Trying to merge FND Types into one variable
data$fndtype <- paste(data$FNDtype, data$FNDtype2, data$FNDtype3, data$FNDtype4, sep = ",")
vars <- c("fndtype")
table <- CreateTableOne(vars = vars, data = data, strata = c("primary_infection"), testNonNormal = kruskal.test, addOverall = TRUE)
kableone(table, nonnormal = vars)
这是 dput 数据集,包括分层变量 postcraniotomy_infection 和我们的患者经历的四种不同症状:
list(postcraniotomy_infection = structure(c(2L, 1L, 2L, 2L, 2L,
1L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L), levels = c("no",
"yes"), class = "factor"), FNDtype = structure(c(1L, 2L, 2L,
1L, 2L, 1L, 8L, 2L, 4L, 4L, 1L, 3L, 4L, 2L, 2L, 4L, 2L, 3L, 1L,
1L, 1L, 3L, 1L, 9L, 7L, 1L, 2L, 2L, 1L, 1L, 5L, 1L, 3L, 1L, 2L,
3L, 3L, 2L, 4L, 2L, 1L, 10L, 2L, 1L, 3L, 6L, 1L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 6L, 1L, 5L, 10L, 1L, 1L, 2L, 10L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 5L, 5L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 5L, 3L,
1L, 3L, 1L), levels = c("none", "hemiparesis", "hypoesthesia",
"aphasia", "visual field defects", "coordiantion disturbance",
"cranial nerve dysfunction", "neglect", "dysarthria", "other"
), class = "factor"), FNDtype2 = structure(c(1L, 1L, 1L, 1L,
3L, 1L, 1L, 5L, 1L, 8L, 1L, 7L, 1L, 4L, 1L, 7L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 8L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 7L,
1L, 6L, 6L, 3L, 1L, 1L, 6L, 1L, 9L, 10L, 1L, 1L, 1L, 1L, 1L,
3L, 1L, 1L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 3L, 4L, 1L, 4L, 9L, 6L, 1L, 4L, 1L, 6L,
1L), levels = c("none", "hemiparesis", "hypoesthesia", "aphasia",
"visual field defects", "coordiantion disturbance", "cranial nerve dysfunction",
"neglect", "dysarthria", "other"), class = "factor"), FNDtype3 = structure(c(1L,
1L, 1L, 1L, 6L, 1L, 1L, 8L, 1L, 1L, 1L, 8L, 1L, 7L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 9L, 8L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 4L, 1L, 1L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 8L, 1L, 6L, 1L, 9L, 1L,
1L, 1L, 1L, 1L), levels = c("none", "hemiparesis", "hypoesthesia",
"aphasia", "visual field defects", "coordiantion disturbance",
"cranial nerve dysfunction", "neglect", "dysarthria", "other"
), class = "factor"), FNDtype4 = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 9L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 8L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 10L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
), levels = c("none", "hemiparesis", "hypoesthesia", "aphasia",
"visual field defects", "coordiantion disturbance", "cranial nerve dysfunction",
"neglect", "dysarthria", "other"), class = "factor"))
我尝试使用带有参数
paste()
的 sep=","
函数,它确实创建了一个变量,其中不同的值用逗号分隔。然而,当我尝试使用 CreateTableOne 创建描述性分析时,结果向我显示了有关症状组合的统计数据(例如,偏瘫、感觉减退、协调障碍 n=xx),而不是有关单一症状的统计数据(例如,偏瘫 n= XX,感觉减退 n=XX,协调障碍 n=XX)。
我真的希望 SO 社区中的有人可以帮助我,因为我坚信这个问题一定有解决方案。如果您需要更多信息,请告诉我!
提前感谢您的支持。
最诚挚的问候,马西莫
您可以使用不同的分类变量来缓解问题。对于每个症状,我们创建一个以全零开头的新列。然后,我们检查每位患者,看看他们在四个 FND 条目中是否有这种症状。如果有,请针对此症状标记为 1。 最后我们创建一个 df ,其中 row 是不同的症状。这些列显示了每种症状在感染/未感染患者中出现的频率。
#install.packages("tableone")
library(tableone)
analyze_fnd_symptoms <- function(data_list) {
# Print diagnostic information
cat("Number of observations:", length(data_list$postcraniotomy_infection), "\n")
cat("Available symptoms:", levels(data_list$FNDtype)[-1], "\n")
# Get number of observations
n <- length(data_list$postcraniotomy_infection)
# Get all symptom levels (excluding "none") from FNDtype
all_symptoms <- levels(data_list$FNDtype)[-1] # Remove "none" which is the first level
# Initialize result dataframe
result_df <- data.frame(
postcraniotomy_infection = data_list$postcraniotomy_infection
)
# Create binary columns for each symptom with explicit comparison
for(symptom in all_symptoms) {
result_df[[symptom]] <- 0 # Initialize with zeros
# Check each FND type and update if symptom is present
for(i in 1:n) {
if(data_list$FNDtype[i] == symptom ||
data_list$FNDtype2[i] == symptom ||
data_list$FNDtype3[i] == symptom ||
data_list$FNDtype4[i] == symptom) {
result_df[i, symptom] <- 1
}
}
# Print diagnostic information for each symptom
cat("Symptom:", symptom, "- Count:", sum(result_df[[symptom]]), "\n")
}
# Verify we have valid columns
valid_cols <- sapply(result_df[,-1], function(x) sum(x) > 0)
valid_symptoms <- names(valid_cols)[valid_cols]
if(length(valid_symptoms) == 0) {
stop("No symptoms found with non-zero counts")
}
# Create table comparing symptoms between infection groups
table <- CreateTableOne(vars = valid_symptoms,
data = result_df,
strata = "postcraniotomy_infection",
addOverall = TRUE)
return(table)
}
# use it
vars <- c("fndtype")
table <- analyze_fnd_symptoms(data)
kableone(table, nonnormal = vars)
输出:
> kableone(table, nonnormal = vars)
整体 | 不 | 是的 | p | 测试 | |
---|---|---|---|---|---|
n | 84 | 37 | 47 | ||
偏瘫(平均值(SD)) | 0.26 (0.44) | 0.41 (0.50) | 0.15 (0.36) | 0.008 | |
感觉减退(平均值(SD)) | 0.18 (0.39) | 0.22 (0.42) | 0.15 (0.36) | 0.430 | |
失语症(平均(SD)) | 0.13 (0.34) | 0.19 (0.40) | 0.09 (0.28) | 0.164 | |
视野缺损(平均值(SD)) | 0.07 (0.26) | 0.11 (0.31) | 0.04 (0.20) | 0.252 | |
协调障碍(平均值(SD)) | 0.12 (0.33) | 0.11 (0.31) | 0.13 (0.34) | 0.787 | |
脑神经功能障碍(平均值(SD)) | 0.07 (0.26) | 0.08 (0.28) | 0.06 (0.25) | 0.764 | |
忽视(平均值(SD)) | 0.11 (0.31) | 0.19 (0.40) | 0.04 (0.20) | 0.031 | |
构音障碍(平均值(SD)) | 0.07 (0.26) | 0.14 (0.35) | 0.02 (0.15) | 0.045 | |
其他(平均值(SD)) | 0.07 (0.26) | 0.16 (0.37) | 0.00 (0.00) | 0.004 |