I used the aggregate function to get the range by factor level. I am trying to rename the columns, but the output from the aggregate function does not have the min and max as separate columns.
# example data
size_cor <- data.frame(SpCode = rep(c(200, 400, 401), 3),
Length = c(45, 23, 56, 89, 52, 85, 56, 45, 78))
# aggregate function
spcode_range <- with(size_cor, aggregate(Length, list(SpCode), FUN = range))
Output:
spcode_range
Group.1 x.1 x.2
1 200 45 89
2 400 23 52
3 401 56 85
Data structure:
str(spcode_range)
'data.frame': 3 obs. of 2 variables:
$ Group.1: num 200 400 401
$ x : num [1:3, 1:2] 45 23 56 89 52 85
dim(spcode_range)
[1] 3 2
The output has three columns: Group.1
, x.1
(min) and x.2
(max), but the dataframe has only 2 columns. I have tried setNames, rename and name with no success because I am trying to name three columns when R has only 2 columns.