hadley / reshape

An R package to flexible rearrange, reshape and aggregate data
http://had.co.nz/reshape
Other
210 stars 58 forks source link

Allow specifying data type of value column #57

Open krlmlr opened 10 years ago

krlmlr commented 10 years ago

This is kind of the opposite to #51. In melt, the value column is coerced to character if only one of the measure.vars is a character. Perhaps it would make sense to introduce a parameter that specifies the desired output type, and to warn if such coercion happens. Something like melt(d, coerce = as.numeric) or melt(d, target_type = "numeric") would substitute values that cannot be coerced with NA (with a warning) and turn off the warning that value types are mixed.

hadley commented 10 years ago

You might want to look at tidyr...

krlmlr commented 10 years ago

Thanks, I wasn't aware of tidyr. I see that gather() has a convert parameter, but according to the code and the docs it doesn't seem to apply to the current use case. I propose to fix the type of the value column, i.e., to coerce it to a given type, while convert = TRUE will call type.convert() on the key column.

Perhaps tidyr::gather() could also benefit from fixing the value of the type column.