[R] interesting feature

Liaw, Andy andy_liaw at merck.com
Wed Feb 18 19:00:15 CET 2004


> From: Svetlana Eden
> 
> Hi, everybody.
> This was an interesting discussion last time and it helped me a lot.
> 
> Could you please have a look at some feature and tell me
> why it was designed this way
> (my questions are under #########)

I give it a shot...
 
> > x = c(1, 10)
> > y = c(99, 55)
> > d <- data.frame(x = x, y = y)
> > d
>    x  y
> 1  1 99
> 2 10 55
> > add <- data.frame(x = 14, y = 99)
> > add
>    x  y
> 1 14 99
> > d <- rbind(d, add)
> > d
>     x  y
> 1   1 99
> 2  10 55
> 11 14 99
> ######### it would be more natural to index the rows: 1,2,3 instead of
> #1,2,11  ?!

rownames for data.frame need not be 1, 2, ..., and we need something that's
going to work regardless.

> > d[3,1]
> [1] 14
> > d[11,1]
> [1] NA
> ######### especially if index '11' is not functioning...

This one seems curious to me.  Trying to access non-existing column result
in error:

> d[1,11]
Error in "[.data.frame"(d, 1, 11) : undefined columns selected

OTOH, try:

> d[11,1] <- 1
> dim(d)
[1] 11  2
> d
Error in data.frame(x = c(" 1", "10", "14", "NA", "NA", "NA", "NA", "NA",  :

        duplicate row.names: 11

so that destroys the integrity of the data frame!

> > add1 <- data.frame(x = 10, y = 87)
> > d <- rbind(d, add)
> ######### now I would think that the next index should be 21, BUT:
> > d
>     x  y
> 1   1 99
> 2  10 55
> 11 14 99
> 12 10 87
> ######### so what is the intuition of such indexing?

I believe the rationale is explained in ?make.unique (which
rbind.data.frame() calls to create the rownames).
 
HTH,
Andy

 
> -- 
> Svetlana Eden        Biostatistician II            School of Medicine
>                      Department of Biostatistics   Vanderbilt 
> University
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
> 


------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list