简体   繁体   English

dplyr总结str.default(obj,...)中的错误dims [product 11]与对象的长度不匹配[3]

[英]dplyr summarise Error in str.default(obj, …) dims [product 11] do not match the length of object [3]

I am having a very frustrating problem using dplyr group_by and summarise functions. 我使用dplyr group_bysummarise函数时遇到了一个非常令人沮丧的问题。

This is my dataset: 这是我的数据集:

> 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 ...

I now want to calculate some summary stats by Treatment so: 我现在想通过治疗计算一些摘要统计数据:

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

Which gives me: 这给了我:

> 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

So far so good. 到现在为止还挺好。 However, if I try and graph this data using ggplot I get the following error: 但是,如果我尝试使用ggplot绘制此数据,我会收到以下错误:

> 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

The str of the dataframe gives this: 数据帧的str给出了:

> 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]

I don't understand what is going on here! 我不明白这里发生了什么! Can anyone help out? 任何人都可以帮忙吗?

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

在此输入图像描述

I just ran into this same issue and solved it by adding a mutate_if at the end, in case it helps: 我刚刚遇到了同样的问题并通过在最后添加mutate_if来解决它,以防它有所帮助:

df2<- df%>% 
  group_by(group) %>% 
  mutate_each(funs(scale, mean)) %>% 
  mutate_if(is.matrix, as.vector)

could the error message not be related to the fact that your Treatment has 4 levels? 错误信息可能与您的治疗有4个级别的事实无关吗? When it should have 3 levels "U","IU","AN" and levels assigned are 1, 2, 3 and the extra level ".." doesn't have a number assigned to it. 当它应该具有3个级别“U”,“IU”,“AN”和分配的级别是1,2,3并且额外级别“..”没有分配给它的数字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dplyr,dunn测试,dim(robj)&lt;-c(dX,dY)中的错误:dims [产品0]与对象的长度不匹配 - dplyr, dunn test, Error in dim(robj) <- c(dX, dY) : dims [product 0] do not match the length of object R错误:暗淡与物体长度不符 - R error: dims do not match the length of an object R-交叉验证错误处理--“产品变暗与对象长度不匹配” - R - cross validation error handling-- "dims product do not match the length of object" 使用R函数“外部”时出现“尺寸[产品xx]与对象[xx]的长度不匹配”错误 - “dims [product xx] do not match the length of object [xx]” error in using R function `outer` 为什么 R 给我错误“dims [product 28550] do not match the length of object [1]” - Why is R giving me the error “dims [product 28550] do not match the length of object [1]” tbl_summary 错误:dims [产品 18] 与 object [27] 的长度不匹配 - tbl_summary error: dims [product 18] do not match the length of object [27] 使用daply进行频率计数时,R中的“尺寸[积0]与对象长度不匹配”错误 - “dims [product 0] do not match the length of object” error in R when using daply for frequency counts R 重塑 package:“Dim(x) 中的错误”…“dims [产品 100] 与 object [109] 的长度不匹配” - R reshape package: “Error in Dim(x)”… “dims [product 100] do not match the length of object [109]” 你知道为什么尝试pairw.anova时dims [product 0]与object [1]的长度不匹配吗? - Do you know why dims [product 0] do not match the length of object [1] when trying pairw.anova 估算值:VIM软件包(aggr函数)-暗淡[产品284088]与对象的长度不匹配[284121] - Imputing values : VIM package (aggr function) - dims [product 284088] do not match the length of object [284121]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM