Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rid of melt() warning message about type coercion in cases where coercion is wanted #5912

Closed
MoREpro opened this issue Jan 15, 2024 · 2 comments
Labels
reshape dcast melt

Comments

@MoREpro
Copy link

MoREpro commented Jan 15, 2024

This is a feature request:

When the melt function is used on columns containing different data types it is raising the following warning message:

"Warning message:
In melt.data.table(water_AL, id.vars = "", variable.name = "") :
'measure.vars' [ ...] are not all of the same type. By order of hierarchy, the molten data value column will be of type 'character'. All measure variables not of type 'character' will be coerced too. Check DETAILS in ?melt.data.table for more on coercion."

This is for sure helpful when developing a data munging script. But in cases where I have noticed this warning and want this behaviour I wish to have an option to turn off this message.

This could be either implemented by explicitely defining the data type to which the coercion should be done, something like coerce.to="character" or by an option to mute only this message.

@MichaelChirico
Copy link
Member

Filed #5913 which is much more general but would at least partially address your request. I'm not immediately sure of the need for any new arguments -- I might prefer code that's more explicit about coercion for example:

suppressWarnings(melt(DT, ...))

# vs
DT[, (bump_cols) := lapply(.SD, as.character)]
melt(DT, ...)

@MoREpro MoREpro closed this as completed Jan 15, 2024
@tdhock
Copy link
Member

tdhock commented Jan 15, 2024

The proposed coerce.to argument would not be sufficient in some cases with multiple types of corecion, such as below:

> melt(data.table(x1=1:2, x2=c(3,4), y1=c(T,F), y2=0:1), measure=measure(value.name, number, pattern="(.)(.)"))
   number     x     y
   <char> <num> <int>
1:      1     1     1
2:      1     2     0
3:      2     3     0
4:      2     4     1
Messages d'avis :
1: Dans melt.data.table(data.table(x1 = 1:2, x2 = c(3, 4), y1 = c(T,  :
  'measure.vars' [x1, x2] are not all of the same type. By order of hierarchy, the molten data value column will be of type 'double'. All measure variables not of type 'double' will be coerced too. Check DETAILS in ?melt.data.table for more on coercion.
2: Dans melt.data.table(data.table(x1 = 1:2, x2 = c(3, 4), y1 = c(T,  :
  'measure.vars' [y1, y2] are not all of the same type. By order of hierarchy, the molten data value column will be of type 'integer'. All measure variables not of type 'integer' will be coerced too. Check DETAILS in ?melt.data.table for more on coercion.

For that reason I think it is better to specify the type conversion explicitly, prior to calling melt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
reshape dcast melt
Projects
None yet
Development

No branches or pull requests

4 participants