0

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)
gecko
  • 109
  • 5
  • perhaps using `table(x)[unique(x)]`? – dww Jul 29 '21 at 03:01
  • 3
    You can try `tabulate(factor(X, levels = unique(X)))`. – Ritchie Sacramento Jul 29 '21 at 03:03
  • 1
    "based on the number of distinct combinations of variable values" <-- can you provide example data and expected output? that sounds like more than just weighting unique factor levels. – andrew_reece Jul 29 '21 at 03:11
  • 1
    It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jul 29 '21 at 04:28

0 Answers0