[R] colnames from submatrix?

Spencer Graves spencer.graves at pdf.com
Mon Oct 13 18:35:00 CEST 2003


Does the following meet your requirements: 

 > DF <- data.frame(a=1:2, b=c(0,0))
 > DFzeros <- sapply(DF, function(x)all(x==0))
 > names(DF)[!DFzeros]
[1] "a"

hope this helps.  spencer graves

Jeff D. Hamann wrote:

>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