New to R, and in over my head!
I am trying to write code that will combine the following steps:
a) Find the minimum values, per row, between two columns
b) Sum the minimum values found
c) Do this among many columns and construct a pairwise matrix of the results
Steps a & b are easy enough for two columns at a time. Like this:
column1 = c(0.08, 0.20, 0.09, 0.19, 0.25, 0.20, 0.00)
column2 = c(0.07, 0.19, 0.09, 0.21, 0.25, 0.19, 0.00)
ps = data.frame(column1, column2)
sum(pmin(ps$column1,ps$column2))
But for step c, I am having difficulty writing a code that will perform this operation for each pairwise column comparison in a dataframe consisting of 7 rows and 32 columns. This is what I've come up with so far:
d <- replicate(32, rnorm(7))
c <- combn(seq_len(ncol(d)),2)
mat1 <- matrix(0,ncol=32,nrow=32,dimnames=list(colnames(d),colnames(d)))
v1 <- unlist(lapply(seq_len(ncol(c)),function(i) {d1<-d[,c[,i]]; length(which(d1[,1]!=0 & d1[,2]!=0)) }))
mat1[lower.tri(mat1)]<-v1
I am pretty sure my issues lie within the "function" command associated with "v1". But I'm stumped and could really use a bit of help!
Again, my goal is to have a 32x32 matrix of the summed minimum values between each pairwise column comparison.
Does this make sense?
Thank you so much.