KTWillow
KTWillow

Reputation: 307

R - Output of aggregate and range gives 2 columns for every column name - how to restructure?

I am trying to produce a summary table showing the range of each variable by group. Here is some example data:

df <- data.frame(group=c("a","a","b","b","c","c"), var1=c(1:6), var2=c(7:12))

  group var1 var2
1     a    1    7
2     a    2    8
3     b    3    9
4     b    4   10
5     c    5   11
6     c    6   12

I used the aggregate function like this:

df_range <- aggregate(df[,2:3], list(df$group), range)

  Group.1 var1.1 var1.2 var2.1 var2.2
1       a      1      2      7      8
2       b      3      4      9     10
3       c      5      6     11     12

The output looked normal, but the dimensions are 3x3 instead of 5x3 and there are only 3 names:

names(df_range)

[1] "Group.1" "var1"    "var2" 

How do I get this back to the normal data frame structure with one name per column? Or alternatively, how do I get the same summary table without using aggregate and range?

Upvotes: 1

Views: 246

Answers (2)

davechilders
davechilders

Reputation: 9123

Here's an approach using dplyr:

library(dplyr)

df %>%
  group_by(group) %>%
  summarise_each(funs(max(.) - min(.)), var1, var2)

Upvotes: 0

Pierre L
Pierre L

Reputation: 28441

That is the documented output of a matrix within the data frame. You can undo the effect with:

newdf <- do.call(data.frame, df_range)
#  Group.1 var1.1 var1.2 var2.1 var2.2
#1       a      1      2      7      8
#2       b      3      4      9     10
#3       c      5      6     11     12

dim(newdf)
#[1] 3 5

Upvotes: 1

Related Questions