我正在尝试获取数据帧中变量子集的汇总表。子集只关注数值变量。所以我创建了一个仅包含数值变量的子集,并将该子集称为numtable。但是,我收到一条错误消息:
Warning message:
In cbind(Variables, Missing, Type, Min, Max, Mean, SD) :
number of rows of result is not a multiple of vector length (arg 1)
代码如下:
#Summary of the numeric variables in the dataset
numvars <- names(df) %in% c("age_years", "sex", "sop1", "servcode", "los", "admt", "ptstatus", "msdrg",
"msmdc", "sm_er", "diagnosis_order1", "diagnosis_order2", "diagnosis_order3", "diagnosis_order4", "diagnosis_order5",
"diagnosis_order1", "diagnosis_order1", "diagnosis_order1", "diagnosis_order1", "diagnosis_order1", "diagnosis_order1",
"diagnosis_order6", "diagnosis_order7", "diagnosis_order8", "diagnosis_order9", "diagnosis_order10", "diagnosis_order11", "diagnosis_order12",
"diagnosis_order13", "diagnosis_order14", "diagnosis_order15", "diagnosis_order16", "diagnosis_order17", "diagnosis_order18", "diagnosis_order19",
"diagnosis_order20" ,"diagnosis_order21", "diagnosis_order22", "diagnosis_order23", "diagnosis_order24", "diagnosis_order25", "diagnosis_order26",
"diagnosis_order27", "diagnosis_order28", "diagnosis_order29", "diagnosis_order30", "diagnosis_order31", "diagnosis_order32", "diagnosis_order33",
"diagnosis_order34", "diagnosis_order35")
numtable <- df[numvars]
Variables <- names(numtable)
Missing <- sapply(numtable, function(x) sum(is.na(x)))
Type <- sapply(numtable, function(x) class(x))
Min <- sapply(numtable, function(x) min(x, na.rm = TRUE))
Max <- sapply(numtable, function(x) max(x, na.rm = TRUE))
SD <- sapply(numtable, function(x) format(round(sd(x, na.rm=TRUE), 2), nsmall = 2))
Mean <- sapply(numtable, function(x) format(round(mean(x, na.rm=TRUE), 2), nsmall = 2))
#To get the simple table
knitr::kable(as.data.frame(cbind(Variables, Missing, Type, Min, Max, Mean, SD), row.names = FALSE))
#To get the Latex table for the rows
knitr::kable(as.data.frame(cbind(Variables, Missing, Type, Min, Max, Mean, SD), row.names = FALSE), "latex")
假设我有这样的数据帧:
df <- data.frame(age_years = c(33, 11, 45, 67, 8, 99), sex = factor(c(0, 1, 1, 0, 0, 0)))
> df
age_years sex
1 33 0
2 11 1
3 45 1
4 67 0
5 8 0
6 99 0
发布于 2021-03-16 10:29:25
我的猜测是您手动选择的变量之一不是数字。Type
返回什么?它们都返回数字吗?
此外,您还可以动态选择变量,而不是手动选择它们,因为手动选择可能会导致错误。尝试以下方法:
df <- type.convert(df)
numtable <- Filter(is.numeric, df)
Variables <- names(numtable)
Missing <- sapply(numtable, function(x) sum(is.na(x)))
Type <- sapply(numtable, function(x) class(x))
Min <- sapply(numtable, function(x) min(x, na.rm = TRUE))
Max <- sapply(numtable, function(x) max(x, na.rm = TRUE))
SD <- sapply(numtable, function(x) format(round(sd(x, na.rm=TRUE), 2), nsmall = 2))
Mean <- sapply(numtable, function(x) format(round(mean(x, na.rm=TRUE), 2), nsmall = 2))
#To get the simple table
knitr::kable(data.frame(Variables, Missing, Type, Min, Max, Mean, SD))
#To get the Latex table for the rows
knitr::kable(data.frame(Variables, Missing, Type, Min, Max, Mean, SD), "latex")
https://stackoverflow.com/questions/66648249
复制相似问题