I confused when I created the following simple function. If I do not assign the class in the temp then I see nothing on the screen when I execute the function (without print or assigning to anything)
library(dplyr)
library(data.table)
my_percentage <- function(datatable, variable ){
temp <- datatable[, .N, keyby = c(variable)]
temp[, percentage := N/sum(N)]
# temp <- as.data.table(temp)
return(temp)
}
my_percentage(iris %>% as.data.table(), variable = "Species")
# print(my_percentage(iris %>% as.data.table(), variable = "Species"))
However if I assign any class in the object that I will return it will be fine.
my_percentage <- function(datatable, variable ){
temp <- datatable[, .N, keyby = c(variable)]
temp[, percentage := N/sum(N)]
temp <- as.data.table(temp)
return(temp)
}
my_percentage(iris %>% as.data.table(), variable = "Species")
Can anyone explain to me why ?