[Rd] Losing attributes in data.frame() (PR#10873)

gunter.berton at gene.com gunter.berton at gene.com
Mon Mar 3 17:25:09 CET 2008


 
Folks:

Problem: [<-.data,frame()  is losing attributes under certain curcumstances
shown below.

I think this is a bug, at least in documentation, as I was unable to find
explicit documentation of the behavior. Indeed, the only documentation I
found told me attributes are preserved. Here is a detailed description:

> df <- data.frame(a=1:3, b=letters[1:3])
> attr(df,"foo") <- 10
> df
  a b
1 1 a
2 2 b
3 3 c
> attributes(df)
$names
[1] "a" "b"

$row.names
[1] 1 2 3

$class
[1] "data.frame"

$foo
[1] 10

## But now we add a new column using matrix-type indexing

> df[,"c"] <- 10:12
> df
  a b  c
1 1 a 10
2 2 b 11
3 3 c 12
> attributes(df)
$names
[1] "a" "b" "c"

$row.names
[1] 1 2 3

$class
[1] "data.frame"

> ## Note that "foo" attribute is lost; however...


> df <- data.frame(a=1:3, b=letters[1:3])
> attr(df,"foo") <- 10
> df[["c"]] <- 10:12
> attributes(df)
$names
[1] "a" "b" "c"

$row.names
[1] 1 2 3

$foo
[1] 10

$class
[1] "data.frame"

## The foo attribute has been kept this time.

## Details:

> sessionInfo()
R version 2.6.2 (2008-02-08) 
i386-pc-mingw32 

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United
States.1252;LC_MONETARY=English_United
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] graphics  grDevices datasets  grid      tcltk     stats     utils
methods   base     

other attached packages:
[1] svSocket_0.9-5    svIO_0.9-5        svIDE_0.9-5       R2HTML_1.58
svMisc_0.9-5      lme4_0.99875-9   
[7] Matrix_0.999375-4 lattice_0.17-6   
> 


Cheers,

Bert Gunter
Genentech Nonclinical Statistics



More information about the R-devel mailing list