Sum of integer columns is double

603 views Asked by At

I don't understand why and how to prevent the sum of two integer columns to be class numeric, type double. Any idea?

Here is a small working example

library(data.table)

set.seed(123)

A <- rnorm(20, 100, 5)
B <- rnorm(20, 50, 20)

NA.A <- which(A %in% sample(A, 5))
NA.B <- which(B %in% sample(B, 10))

zero.A<- which(A %in% sample(A, 3))
zero.B <- which(B %in% sample(B, 8))

A[NA.A] <- NA
A[zero.A] <- 0
B[NA.B] <- NA
B[zero.B] <- 0

mydt <- data.table(A = as.integer(A), B = as.integer(B))
sapply(mydt, class)
#    A         B 
# "integer" "integer"

mydt[, C := rowSums(.SD, na.rm=T), .SDcols = c("A","B")]
sapply(mydt, class)
#    A         B         C 
# "integer" "integer" "numeric" 

sapply(mydt, typeof)
#    A         B         C 
# "integer" "integer"  "double" 
1

There are 1 answers

1
mtoto On BEST ANSWER

As rowSums()will always return type double, you could alternatively use Reduce() in combination with the + operator to return the new column as integer.

mydt[,C:=Reduce(`+`, lapply(.SD, function(x) ifelse(!is.na(x),x,0))),.SDcols = c("A","B")]