数据帧子集的错误消息:结果行数不是向量长度的倍数(arg 1)

数据帧子集的错误消息:结果行数不是向量长度的倍数(arg 1),r,dataframe,dplyr,R,Dataframe,Dplyr,我试图为我的数据框架中的一部分变量获取一个汇总表。子集只关注数值变量。因此,我创建了一个仅包含数值变量的子集,并将该子集称为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 th

我试图为我的数据框架中的一部分变量获取一个汇总表。子集只关注数值变量。因此,我创建了一个仅包含数值变量的子集,并将该子集称为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")
#数据集中数值变量的摘要

numvars我猜您手动选择的变量之一不是数字。
类型
返回什么?它们是否都返回数字

您还可以动态选择变量,而不是手动选择变量,这可能会导致错误。尝试以下方法:

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")

df Hi,是的,它们都返回数字。在表中,我想把Type作为显示变量类型的列,那么我的猜测是错误的。当你使用我的答案时,你会得到同样的错误吗?我尝试了
df不,当我使用你的答案时,我没有得到任何错误。但是我得到的不是数值,而是整数作为变量的类型。这就是他们的
类的内容。实际上,我也在做同样的事情,字符变量。指示变量是字符还是数字的类型。
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")