On Thu, 28 Aug 2008, Yuan Jian wrote:

Hi,
when I pick out one element from a matrix, the attribute name is kept, but when
more than one elements are extracted, the attribute name lost;

To what attribute 'name' do you refer?

I only see 'dim' and 'dimnames' attributes:

attributes(a)
$dim
[1] 3 3

$dimnames
$dimnames[[1]]
NULL

$dimnames[[2]]
[1] "c1" "c2" "c3"




?
a<-matrix(c(1,2,3,11,12,13,45,56,76),ncol=3,dimnames=list(c(),c("c1","c2","c3")))
k<-a[a[,"c3"]>50,"c3"]
kk<-a[a[,"c3"]>60,"c3"]
attributes(k)
NULL
attributes(kk)
$names
[1] "c3"
?

Try attributes(a[a[,"c3"]>50,"c3", drop = FALSE ] ).

Try adding rownames to 'a', and see what happens to the attributes of each of the above forms.

"[" merely tries to do something reasonable.

Why do you think k should have any attributes, and what names do you think k should have???

HTH,

Chuck

?
YU



        [[alternative HTML version deleted]]



Charles C. Berry                            (858) 534-2098
                                            Dept of Family/Preventive Medicine
E mailto:[EMAIL PROTECTED]                  UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to