I have a series of two-dimensional numerical matrices comprising 1s and 0s. (So I suppose they can also be seen as logical arrays.) What I want to be able to do for such arrays is to generate a vector the length of one dimension of the array (the number of columns). It would contain, for every column in the array, the sum of row totals for the rows where the entry is 1.
Here's what I have for single columns:
#Generate sample data
dataset<-matrix(sample(0:1, size=190, replace=TRUE), nrow=19, ncol=10)
#Calculate row sums
scores<-rowSums(dataset)
#calculate desired statistic for column 1
M1_1 <- sum(scores[which (dataset[,1]==1)])
#calculate same statistic for column 2
M1_2 <- sum(scores[which (dataset[,2]==1)])
Obviously, instead of writing M1_1, M1_2, ..., M1_n, I want to define M1_X to iterate through every column. I suspect it's a really simple thing to do, but haven't been able to figure out how to do it. Any guidance would be appreciated.
We can loop with
sapply
and get thesum
Or using
apply
Or a vectorized approach would be to replicate the 'scores' and multiply it with 'dataset' without making use of any recycling (which can be dangerous at times)
Or another intuitive option is
sweep
Based on OP's post,
Or as @user20650 commented, a concise option is
crossprod
Or without even calculating 'scores' in a different step