[R] lost attrubute:names
Charles C. Berry
cberry at tajo.ucsd.edu
Fri Aug 29 04:02:06 CEST 2008
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:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list