[R] colnames from submatrix?
Liaw, Andy
andy_liaw at merck.com
Mon Oct 13 18:41:58 CEST 2003
If the columns you want to exclude can only contain all 0s (and not some
other values such as 1s), you can do something like:
mat[, colSums(mat != 0) > 0]
to extract the non-zero columns of the matrix. "mat != 0" gives you a
matrix of TRUEs and FALSEs, and colSums() of this matrix tells you how many
non-zero elements are in the columns. You just want columns with at least
one non-zero entry.
HTH,
Andy
> -----Original Message-----
> From: Jeff D. Hamann [mailto:jeff_hamann at hamanndonald.com]
> Sent: Monday, October 13, 2003 12:22 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] colnames from submatrix?
>
>
> Hi R-Wizards:
>
> I've looking through the R docs and have yet to find what I'm
> looking for and have tried a few intermediate steps to now
> avail yet and rather than spend another few hours looking for
> the solution, I figured I would post a message.
>
> I have a matrix (actually a set of them) that I want to pull
> all the names of the non-zero columns into a vector/list for
> further processing:
>
> d0 d1 d2 s0 s1 s2 s3
> [1,] -4.4721360 -447.29878969 -436.18978037 0 0 0 0
> [2,] 0.2236068 -25.83121172 -29.21571675 0 0 0 0
> [3,] 0.2236068 0.13009088 42.49338060 0 0 0 0
> [4,] 0.2236068 0.17155235 0.04891280 0 0 0 0
> [5,] 0.2236068 -0.08027479 -0.14749423 0 0 0 0
> [6,] 0.2236068 -0.02394758 -0.12845820 0 0 0 0
> [7,] 0.2236068 0.03838012 -0.15270556 0 0 0 0
> [8,] 0.2236068 0.18150155 -0.17073267 0 0 0 0
> [9,] 0.2236068 -0.14047328 -0.11013806 0 0 0 0
> [10,] 0.2236068 -0.34247697 -0.05627832 0 0 0 0
> [11,] 0.2236068 -0.27058719 0.31379811 0 0 0 0
> [12,] 0.2236068 -0.04930450 0.41093718 0 0 0 0
> [13,] 0.2236068 0.10968920 0.32996356 0 0 0 0
> [14,] 0.2236068 -0.05104658 0.08743581 0 0 0 0
> [15,] 0.2236068 -0.19184524 -0.29548841 0 0 0 0
> [16,] 0.2236068 -0.06285400 -0.26123732 0 0 0 0
> [17,] 0.2236068 -0.52558878 -0.34821174 0 0 0 0
> [18,] 0.2236068 0.15258305 -0.10895070 0 0 0 0
> [19,] 0.2236068 0.22044668 -0.21442723 0 0 0 0
> [20,] 0.2236068 0.51934865 -0.41193918 0 0 0 0
>
>
> such that I only end up with a list of names (d0,d1,d2). I've
> been using the qr( x )$rank to get the number of non-zero
> columns, but I'm not sure how to only return the names of the
> non-zero columns as
>
> > colnames( qr( attr( eval( nlsystemols$eq[[1]]$deriv ), "gradient" )
> > )$qr )
>
> will return
>
> > [1] "d0" "d1" "d2" "s0" "s1" "s2" "s3"
>
> Is/Are there one of those great S or R function/shortcuts
> that will return a
> submatrix(?) or a partioned matrix such that when I call
> colnames() I get:
>
> > [1] "d0" "d1" "d2"
>
> Thanks,
> Jeff.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
>
More information about the R-help
mailing list