I want to count
distinct values of var2 grouping by var1 in a .xdf file,
I tried something like this
myFun <- function(dataList) {
UniqueLevel <<- unique(c(UniqueLevel, dataList$var2))
SumUniqueLevel <<- length(UniqueLevel)
return(NULL)
}
rxSummary(formula = ~ var1,
data = "DefModelo2.xdf",
transformFunc = myFun,
transformObjects = list(UniqueLevel = NULL),
removeZeroCounts = F)
Thank you in advance
EDIT:
Probably using RevoPemaR is the the faster way
Split by
var1
, and then for each group, count up the unique values ofvar2
. This assumes thatvar1
andvar2
are factors, if they're not you'll have to runrxFactors
first.Or you could get my dplyrXdf package and use a dplyr group_by/summarise pipeline (which basically does all the above, including converting to factors if necessary):
One other option is to use
rxCrossTabs
. This way you get a cross-tabulation of the two factors, and you can just count non zero entries to determine unique values by one of the factors.