Home > Software design >  How to calculate vector pairwise comparisons using ifesle function
How to calculate vector pairwise comparisons using ifesle function

Time:11-04

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)))

data enter image description here

Thank you for your help.

CodePudding user response:

how about something like this:

a = 3*runif(100) # I'm just randomizing values, your dataset should come here

b = expand.grid(a,a) #also 'merge' would work
b$digital = 0
b$digital[abs(b[,1]-b[,2])>1] = 1 
result = b$digital

I don't know what you meant by putting values into matrix, but the 0/1 values are in the result variable (in a vector form still). This is by no means the simplest and smoothest solution though, just a fast one:) Also I didn't use ifelse function, but abs(b[,1]-b[,2])>1 is a boolean vector and the final result is same as with ifelse function..

CodePudding user response:

Using mtcars as example data, I'd do it like this:

data = mtcars

col_pairs = combn(names(data), 2, simplify = TRUE)

counts = apply(col_pairs, MARGIN = 2, function(x) sum(abs(data[[x[1]]] - data[[x[2]]]) > 1))

result = matrix(
  NA,
  nrow = ncol(data), ncol = ncol(data),
  dimnames = list(names(data), names(data))
)

result[t(col_pairs)] = counts
result
#      mpg cyl disp hp drat wt qsec vs am gear carb
# mpg   NA  32   32 32   32 32   27 32 32   32   32
# cyl   NA  NA   32 32   21 30   32 32 32   20   30
# disp  NA  NA   NA 32   32 32   32 32 32   32   32
# hp    NA  NA   NA NA   32 32   32 32 32   32   32
# drat  NA  NA   NA NA   NA 16   32 32 32    3   19
# wt    NA  NA   NA NA   NA NA   32 28 28   17   19
# qsec  NA  NA   NA NA   NA NA   NA 32 32   32   32
# vs    NA  NA   NA NA   NA NA   NA NA  0   32   20
# am    NA  NA   NA NA   NA NA   NA NA NA   32   21
# gear  NA  NA   NA NA   NA NA   NA NA NA   NA   14
# carb  NA  NA   NA NA   NA NA   NA NA NA   NA   NA
  • Related