I'd like to specify the class of output columns in melt
(or gather
). And I would like to do it for all columns, and different classes.
For example, I have some data:
example <- data.frame(day = c(1, 2), max = c(20, 21), min = c(1, 2))
> example
day max min
1 1 20 1
2 2 21 2
I melt those data
exmelt <- melt(example, id.vars = "day", variable.name = "minmax", value.name = "temp")
> exmelt
day minmax temp
1 1 max 20
2 2 max 21
3 1 min 1
4 2 min 2
> str(exmelt)
'data.frame': 4 obs. of 3 variables:
$ day : num 1 2 1 2
$ minmax: Factor w/ 2 levels "max","min": 1 1 2 2
$ temp : num 20 21 1 2
Say I would like day
to be class factor and temp
to be class integer
I can do this after melting with as.factor()
exmelt$day <- as.factor(exmelt$day)
exmelt$temp <- as.integer(exmelt$temp)
> str(exmelt)
'data.frame': 4 obs. of 3 variables:
$ day : Factor w/ 2 levels "1","2": 1 2 1 2
$ minmax: Factor w/ 2 levels "max","min": 1 1 2 2
$ temp : int 20 21 1 2
To do this afterward for a complex data frame of many columns and different classes, some factors, some integers, etc., is going to be tedious and messy.
Is there w way to include this in melt
? Like e.g.
melt(example,
id.vars = "day",
variable.name = "minmax",
value.name = "temp",
colClasses = c("factor", "factor", "integer"))
We can use the
melt
fromdata.table
which also has the optionsvariable.factor
andvalue.factor
. Other than that, thecolClasses
is not an argument in it.But, suppose if we need to do this in a single step, create a
vector
of functions and then apply it usingMap
andget