I have a data.table called enc.per.day for encounters per day. It has 2403 rows in which a date of service is specified and the number of patients seen on that day. I wanted to see the median number of patients seen on any type of weekday.
enc.per.day[,list(patient.encounters=median(n)),by=list(weekdays(DOS))]
That line gives an error
Error in
[.data.table
(enc.per.day, , list(patient.encounters = median(n)), : columns of j don't evaluate to consistent types for each group: result for group 4 has column 1 type 'integer' but expecting type 'double'
The following all work well
tapply(enc.per.day$n,weekdays(enc.per.day$DOS),median)
enc.per.day[,list(patient.encounters=round(median(n))),by=list(weekdays(DOS))]
enc.per.day[,list(patient.encounters=median(n)+0),by=list(weekdays(DOS))]
What is going on? It took me a long time to figure out why my code would not work.
By the way the underlying vector enc.per.day$n is an integer
storage.mode(enc.per.day$n)
returns "integer". Further, there are no NAs anywhere in the data.table.
See Question&Answers more detail:os