Skip to content

Document type conversion of data.table/fill #2617

@MichaelChirico

Description

@MichaelChirico

It appears dcast will attempt to convert the value of fill to match that of value.var; I think this is right/probably expected, but should probably be added to ?dcast.data.table anyway (was wondering whether I needed to say fill = 0L or would fill = 0 be fine to get integer output):

# c integer, fill numeric: no need to specify fill = 0L
DT = data.table(a = c(1L, 1L, 2L), b = c(1L, 2L, 2L), c = c(1L, 1L, 1L))
dcast(DT, a ~ b, value.var = 'c', fill = 0)[ , sapply(.SD, class)]
#         a         1         2 
# "integer" "integer" "integer" 

# c numeric, fill integer: no attempt to convert c's output to integer due to fill's class
DT = data.table(a = c(1L, 1L, 2L), b = c(1L, 2L, 2L), c = c(1.1, 1.2, 1.3))
dcast(DT, a ~ b, value.var = 'c', fill = 0L)[ , sapply(.SD, class)]
#         a         1         2 
# "integer" "numeric" "numeric" 

# c numeric, fill character: it seems as.numeric('a') causes the failed conversion warning
DT = data.table(a = c(1L, 1L, 2L), b = c(1L, 2L, 2L), c = c(1.1, 1.2, 1.3))
dcast(DT, a ~ b, value.var = 'c', fill = 'a')[ , sapply(.SD, class)]
#         a         1         2 
# "integer" "numeric" "numeric" 

Warning message: In dcast.data.table(DT, a ~ b, value.var = "c", fill = "a") : NAs introduced by coercion

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions