简体   繁体   中英

Creating a matrix from operations done on another dataset

I have a large dataset, X with 58140 columns, filled with either 1 or 0

I would like to create a 58139 x 58139 matrix from the information of the 58139 columns in the dataset.

For each Aij in the matrix I would like to find the number of common rows which contain the value 1 for Column i+1 and Column J+1 from X.

I figured I can do this through sum(X[[2]]+X[[3]] == 2) for the A12 element of the matrix.

The only problem left is a way to code the matrix in.

You can use mapply . That returns a numeric vector. Then you can just wrap it in a call to matrix and ignore the first row and column.

# sample data
set.seed(123)
X <- data.frame(matrix(rbinom(200, 1, .5), nrow=10))
#
A <- matrix(mapply(function(i, j) sum(rowSums(X[, c(i,j)])==2), 
                   i=rep(1:ncol(X), ncol(X)), 
                   j=rep(1:ncol(X), each=ncol(X))), 
            ncol=ncol(X))[-1, -1]
A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM