setbackademic
setbackademic

Reputation: 143

Different output when printed vs saved as a data frame

I'm trying to create a summary data frame which has averages, mins and maxes (see original post here). I can print the output I want, but whenever I try to save it to a data frame, I only get the averages, not the mins and maxes. I've tried updating R and tidyr, and I can't think of anything else that would cause this. I've tried using as.data.frame() and that doesn't help.

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

Upvotes: 0

Views: 50

Answers (1)

Sharadnanda Mondal
Sharadnanda Mondal

Reputation: 11

it works fine..

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

Upvotes: 1

Related Questions