gunter.berton at gene.com
2008-Mar-03 16:25 UTC
[Rd] Losing attributes in data.frame() (PR#10873)
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 > dfa 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 > dfa 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
ripley at stats.ox.ac.uk
2008-Mar-04 08:45 UTC
[Rd] Losing attributes in data.frame() (PR#10873)
This is a delicate balance. The crucial part (which you didn't mention) is that this only happens if you add a column by df[, "c"] <- value ; if you replace the contents of a column then attributes are preserved. Clearly some attributes like names are invalidated when you extend a list, and others may or may not be. Some parts of R copy all attributes which are not obviously invalidated (see mostattributes<-) and others only copy those that are obviously valid (and the balance has swung over the years towards the first camp). I'm in the camp that says we are too generous in copying over attributes, but here I will make [<- consistent with [[<- and $<-. Note that e.g. df[, 1:2] drops the attribute, whereas df[,] does not, something inherited from lists (and vectors):> x <- structure(list(a=1,b=2), foo=10) > attributes(x[1:2])$names [1] "a" "b"> attributes(x[])$names [1] "a" "b" $foo [1] 10 On Mon, 3 Mar 2008, gunter.berton at gene.com wrote:> > 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.I don't think you can expect such detailed documentation -- given how little use is made of the documentation we do write no one is going to volunteer to write up (and maintain the write up of) such details.> 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 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595