首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >dataframe子集的错误消息:结果行数不是向量长度的倍数(arg 1)

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

Stack Overflow用户
提问于 2021-03-16 10:20:34
回答 1查看 29关注 0票数 0

我正在尝试获取数据帧中变量子集的汇总表。子集只关注数值变量。所以我创建了一个仅包含数值变量的子集,并将该子集称为numtable。但是,我收到一条错误消息:

代码语言:javascript
运行
复制
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)

代码如下:

代码语言:javascript
运行
复制
#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")

假设我有这样的数据帧:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

回答已采纳

发布于 2021-03-16 10:29:25

我的猜测是您手动选择的变量之一不是数字。Type返回什么?它们都返回数字吗?

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

代码语言:javascript
运行
复制
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")
票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66648249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档