首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >str.default(obj,...)中的dplyr摘要错误dims [产品11]与对象[3]的长度不匹配

str.default(obj,...)中的dplyr摘要错误dims [产品11]与对象[3]的长度不匹配
EN

Stack Overflow用户
提问于 2016-07-28 22:26:59
回答 3查看 979关注 0票数 1

我在使用dplyr group_bysummarise函数时遇到了一个非常令人沮丧的问题。

这是我的数据集:

代码语言:javascript
复制
> cum_ems_totals
Source: local data frame [12 x 4]

   Chamber Total_emmissions Treatment  Block
    <fctr>            <dbl>    <fctr> <fctr>
1        1        5769.0507         U      1
2        3        7790.1426        IU      1
3        4        5166.8992        AN      1
4        5        7625.7319        AN      2
5        6        1964.0970        IU      2
6        7        5052.1268         U      2
7        9        4207.5324        IU      3
8       10         470.7014        AN      3
9       12        5675.9171         U      3
10      14        5666.1678         U      4
11      15        2134.5002        AN      4
12      16        4093.4687        IU      4

> str(cum_ems_totals)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   12 obs. of  4 variables:
 $ Chamber         : Factor w/ 13 levels "1","3","4","5",..: 1 2 3 4 5 6 7 8 9 11 ...
 $ Total_emmissions: num [1:101, 1] 5769 7790 5167 7626 1964 ...
 $ Treatment       : Factor w/ 4 levels "U","IU","AN",..: 1 2 3 3 2 1 2 3 1 1 ...
 $ Block           : Factor w/ 5 levels "1","2","3","13",..: 1 1 1 2 2 2 3 3 3 5 ...

我现在想通过处理来计算一些汇总统计数据,所以:

代码语言:javascript
复制
cum_ems_summary <- cum_ems_totals %>% filter(Chamber != "10") %>% 
  group_by(Treatment) %>% 
  summarise(n = n(), Mean = mean(Total_emmissions, na.rm = TRUE),
                      SD = sd(Total_emmissions, na.rm = TRUE), SEM = SD/sqrt(n))

这给了我:

代码语言:javascript
复制
> cum_ems_summary
Source: local data frame [3 x 5]

  Treatment     n     Mean        SD       SEM
     <fctr> <int>    <dbl>     <dbl>     <dbl>
1         U     4 5540.816  329.0763  164.5381
2        IU     4 4513.810 2415.6355 1207.8178
3        AN     3 4975.710 2750.6038 1588.0618

到目前一切尚好。但是,如果我尝试使用ggplot绘制这些数据,我会得到以下错误:

代码语言:javascript
复制
> ggplot(cum_ems_summary, aes(x = Treatment, y = Mean, fill = Treatment)) + geom_bar(stat = "identity")
Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 3, 11

数据帧的str提供了以下内容:

代码语言:javascript
复制
> str(cum_ems_summary)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   3 obs. of  5 variables:
 $ Treatment: Factor w/ 4 levels "U","IU","AN",..: 1 2 3
 $ n        : int  4 4 3
 $ Mean     :
Error in str.default(obj, ...) : 
  dims [product 11] do not match the length of object [3]

我不明白这是怎么回事!有人能帮上忙吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-07-28 22:52:13

代码语言:javascript
复制
#Reproduce error
str(cum_ems_summary)
# Error in str.default(obj, ...) : 
#   dims [product 11] do not match the length of object [3]

#Fix
cum_ems_totals$Total_emmissions <- c(cum_ems_totals$Total_emmissions)


#Try again
cum_ems_summary <- cum_ems_totals %>% filter(Chamber != "10") %>% 
  group_by(Treatment) %>% 
  summarise(n = n(), Mean = mean(Total_emmissions, na.rm = TRUE),
            SD = sd(Total_emmissions, na.rm = TRUE), SEM = SD/sqrt(n))

ggplot(cum_ems_summary, aes(x = Treatment, y = Mean, fill = Treatment)) + geom_bar(stat = "identity")

票数 3
EN

Stack Overflow用户

发布于 2017-06-23 22:12:51

我刚刚遇到了同样的问题,并通过在末尾添加一个mutate_if来解决它,以防它有帮助:

代码语言:javascript
复制
df2<- df%>% 
  group_by(group) %>% 
  mutate_each(funs(scale, mean)) %>% 
  mutate_if(is.matrix, as.vector)
票数 1
EN

Stack Overflow用户

发布于 2016-07-28 22:46:09

该错误消息是否与您的治疗具有4个级别的事实无关?当它应该有3个级别"U","IU","AN“,并且分配的级别是1,2,3和额外的级别"..”没有给它分配号码。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38639289

复制
相关文章

相似问题

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