mercredi 3 novembre 2021

How to calculate vector pairwise comparisons using ifesle function

I need to calculate all pairwise difference for many variables (there are 100 in my dataset). If the abs difference is >1 then it should appear 1, otherwise the result should be 0. Then I want to summarize the 1 and 0 values for each of the pairwise comparisons and arrange them into a matrix. I have tried the following code but there is an error message.

 juan<-outer(seq_along(rio_csv), seq_along(rio_csv), FUN =
  Vectorize(function(i, j)(sum(ifelse(abs(rio_csv[[i]]-rio_csv[[j]]))>1,1,0))))

The operation works well for two columns though.

> (pri<-sum(ifelse(abs(rio_csv$V1-rio_csv$V2)>1,1,0)))

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire