打印时输出的输出不同,另存为数据帧

时间:2017-02-27 00:53:37

标签: r dplyr

我正在尝试创建一个包含平均值,分钟数和最大值(see original post here)的摘要数据框。我可以打印我想要的输出,但每当我尝试将其保存到数据框时,我只得到平均值,而不是分钟和最大值。我已经尝试更新R和tidyr,我想不出任何会导致这种情况的事情。我尝试过使用as.data.frame()但这没有帮助。

#example df
df <-  read.table(header=TRUE, text="shop    tables  chairs  beds
                  jim-1   2   63  31
                  jim-2a  10  4   16
                  jim-2b  32  34  43
                  jen-1   32  90  32
                  jen-2   73  91  6
                  jen-3   35  85  65
                  sam-a   72  57  72
                  sam-b   18  48  11
                  sam-c   34  49  79
                  paul-1  43  49  23
                  paul-2  76  20  23
                  paul-2a 34  20  8")

#create a grouping to allow me to average out group values
shop_group = sub("-.*", "", df$shop)

#print a summary table (works fine)
aggregate(df[,2:4], list(shop_group), 
          FUN = function(x) summary(x)[c(4,1,6)])

#generate a summary data frame (doesn't work, only gives me the averages, not the mins and maxes)
summ_df= aggregate(df[,2:4], list(shop_group), 
                 FUN = function(x) summary(x)[c(4,1,6)])

1 个答案:

答案 0 :(得分:1)

它工作得很好..

> aggregate(df[,2:4], list(shop_group), 
+           FUN = function(x) summary(x)[c(4,1,6)])
  Group.1 tables.Mean tables.Min. tables.Max. chairs.Mean chairs.Min. chairs.Max.
1     jen       46.67       32.00       73.00       88.67       85.00       91.00
2     jim       14.67        2.00       32.00       33.67        4.00       63.00
3    paul       51.00       34.00       76.00       29.67       20.00       49.00
4     sam       41.33       18.00       72.00       51.33       48.00       57.00
  beds.Mean beds.Min. beds.Max.
1     34.33      6.00     65.00
2     30.00     16.00     43.00
3     18.00      8.00     23.00
4     54.00     11.00     79.00
> summ_df= aggregate(df[,2:4], list(shop_group), 
+                    FUN = function(x) summary(x)[c(4,1,6)])
> summ_df
  Group.1 tables.Mean tables.Min. tables.Max. chairs.Mean chairs.Min. chairs.Max.
1     jen       46.67       32.00       73.00       88.67       85.00       91.00
2     jim       14.67        2.00       32.00       33.67        4.00       63.00
3    paul       51.00       34.00       76.00       29.67       20.00       49.00
4     sam       41.33       18.00       72.00       51.33       48.00       57.00
  beds.Mean beds.Min. beds.Max.
1     34.33      6.00     65.00
2     30.00     16.00     43.00
3     18.00      8.00     23.00
4     54.00     11.00     79.00
>