[R] delete selecting rows and columns

Kaskelma, Heikki kashei at sip-oy.com
Tue Mar 6 19:36:25 CET 2007


A one-dimensional delete might be faster:

n <- 4000
m <- 0.01
matr <- matrix(1:(n^2), n, n)
excl_r <- sample(n, m*n)
incl_r <- setdiff(1:n, excl_r)
excl_c <- sample(n, m*n)
incl_c <- setdiff(1:n, excl_c)
system.time(matr[-excl_r, -excl_c])
system.time(
{ m4 <- matr[outer(incl_r, incl_c, function(i, j) i + n*(j - 1)) ]
  dim(m4) <- c(length(incl_r), length(incl_c))
}          )


Heikki Kaskelma


jastar wrote:
> 
> Hi,
> I'm working with a big square matrix (15k x 15k) and I have 
> some trouble.
> I want to delete selecting rows and columns.
> I'm using something like this:
> 
>> sel_r=c(15,34,384,985,4302,6213)
>> sel_c=c(3,151,324,3384,7985,14302)
>> matrix=matrix[-sel_r,-sel_c]
> 
> but it works very slow.
> Does anybody know how to make it in faster way?



More information about the R-help mailing list