Andreas Borg
2010-Dec-07 11:34 UTC
[R] How to find out if a data frame has automatic row names?
Hi all, I just stumbled across the difference between explicit and 'automatic' row names for data frames. Is there a quick way to find out if a data frame has automatic row names or not? Of course I know this for data frames that I create myself, but what if a function needs to extract this information for an argument? The following example shows that this property is not easily visible: > # data frame with automatic row names > df <- data.frame(a=1:2, b=3:4) > # data frame with explicit row names > df2 <- data.frame(a=1:2, b=3:4, row.names=1:2) > # printing does not reveal any difference > df a b 1 1 3 2 2 4 > df2 a b 1 1 3 2 2 4 > # both are considered equal > all.equal(df, df2) [1] TRUE > identical(df, df2) [1] TRUE > # calling rownames gives the same result > rownames(df) [1] "1" "2" > rownames(df2) [1] "1" "2" > # when converting to a matrix, it makes a difference > as.matrix(df) a b [1,] 1 3 [2,] 2 4 > as.matrix(df2) a b 1 1 3 2 2 4 Thanks for any suggestion, Andreas -- Andreas Borg Medizinische Informatik UNIVERSIT?TSMEDIZIN der Johannes Gutenberg-Universit?t Institut f?r Medizinische Biometrie, Epidemiologie und Informatik Obere Zahlbacher Stra?e 69, 55131 Mainz www.imbei.uni-mainz.de Telefon +49 (0) 6131 175062 E-Mail: borg at imbei.uni-mainz.de Diese E-Mail enth?lt vertrauliche und/oder rechtlich gesch?tzte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrt?mlich erhalten haben, informieren Sie bitte sofort den Absender und l?schen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.
Sarah Goslee
2010-Dec-07 12:03 UTC
[R] How to find out if a data frame has automatic row names?
If as.matrix distinguishes between the two options, then you could look at the code for that function and see how it's done. Sarah On Tue, Dec 7, 2010 at 6:34 AM, Andreas Borg <andreas.borg at unimedizin-mainz.de> wrote:> Hi all, > > I just stumbled across the difference between explicit and 'automatic' row > names for data frames. Is there a quick way to find out if a data frame has > automatic row names or not? Of course I know this for data frames that I > create myself, but what if a function needs to extract this information for > an argument? > > The following example shows that this property is not easily visible: > >> # data frame with automatic row names >> df <- data.frame(a=1:2, b=3:4) >> # data frame with explicit row names >> df2 <- data.frame(a=1:2, b=3:4, row.names=1:2) >> # printing does not reveal any difference >> df > ?a b > 1 1 3 > 2 2 4 >> df2 > ?a b > 1 1 3 > 2 2 4 >> # both are considered equal >> all.equal(df, df2) > [1] TRUE >> identical(df, df2) > [1] TRUE >> # calling rownames gives the same result >> rownames(df) > [1] "1" "2" >> rownames(df2) > [1] "1" "2" >> # when converting to a matrix, it makes a difference >> as.matrix(df) > ? ?a b > [1,] 1 3 > [2,] 2 4 >> as.matrix(df2) > ?a b > 1 1 3 > 2 2 4 > > Thanks for any suggestion, > > Andreas > > -- > Andreas Borg > Medizinische Informatik >-- Sarah Goslee\ http://www.functionaldiversity.org
Peter Ehlers
2010-Dec-07 12:26 UTC
[R] How to find out if a data frame has automatic row names?
On 2010-12-07 03:34, Andreas Borg wrote:> Hi all, > > I just stumbled across the difference between explicit and 'automatic' > row names for data frames. Is there a quick way to find out if a data > frame has automatic row names or not? Of course I know this for data > frames that I create myself, but what if a function needs to extract > this information for an argument? > > The following example shows that this property is not easily visible: > > > # data frame with automatic row names > > df<- data.frame(a=1:2, b=3:4) > > # data frame with explicit row names > > df2<- data.frame(a=1:2, b=3:4, row.names=1:2) > > # printing does not reveal any difference > > df > a b > 1 1 3 > 2 2 4 > > df2 > a b > 1 1 3 > 2 2 4 > > # both are considered equal > > all.equal(df, df2) > [1] TRUE > > identical(df, df2) > [1] TRUE > > # calling rownames gives the same result > > rownames(df) > [1] "1" "2" > > rownames(df2) > [1] "1" "2" > > # when converting to a matrix, it makes a difference > > as.matrix(df) > a b > [1,] 1 3 > [2,] 2 4 > > as.matrix(df2) > a b > 1 1 3 > 2 2 4 > > Thanks for any suggestion, >Check ?.row_names_info Peter Ehlers> Andreas >
Andreas Borg
2010-Dec-07 13:54 UTC
[R] How to find out if a data frame has automatic row names?
That does it. Thanks!> > Check ?.row_names_info > > Peter Ehlers > >> Andreas >> > >-- Andreas Borg Medizinische Informatik UNIVERSIT?TSMEDIZIN der Johannes Gutenberg-Universit?t Institut f?r Medizinische Biometrie, Epidemiologie und Informatik Obere Zahlbacher Stra?e 69, 55131 Mainz www.imbei.uni-mainz.de Telefon +49 (0) 6131 175062 E-Mail: borg at imbei.uni-mainz.de Diese E-Mail enth?lt vertrauliche und/oder rechtlich gesch?tzte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrt?mlich erhalten haben, informieren Sie bitte sofort den Absender und l?schen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.
Seemingly Similar Threads
- Using missing() in a S4 method with extra arguments
- Problem with documentation of user-defined operator (S4 method)
- Package with multiple shared libraries
- [R] Sweave stops when opening X11 device fails
- Feature request: txtProgressBar with ability to write to arbitrary stream