I am trying to create weights for an analysis based on the number of distinct combinations of variable values in a dataset. I tried using the distinct()
and count()
functions but the order of the rows is disrupted (same with using rle()
. The function below calculates the weights accurately and in the correct order but it is not very efficient. Is there a way to make it faster? Thanks!
weights<-function(v){
sum(X==v)
}
w<-sapply(unique(X, weights)