In playing around with data.frames (and wanting a simple, cheap way to use the variable and case names in plots; but I've solved that with some hacks, yech), I noticed the following behavior with subsetting. testdata <- data.frame(matrix(1:20,nrow=4,ncol=5)) names(testdata) ## expect labels, get them names(testdata[2,]) ## expect labels, get them names(testdata[,2]) ## expect labels, but NOT -- STRIPPED OFF?? testdata[,2] ## would have expect a name (X2) in the front? NOT EXPECTED testdata[2,] ## get what I expect testdata[2,2] ## just a number, not a sub-data.frame? unexpected testdata[2,2:3] ## this is a data.frame testdata[2:3,2:3] ## and this is, too.> version_ platform i386-pc-linux-gnu arch i386 os linux-gnu system i386, linux-gnu status alpha major 1 minor 8.0 year 2003 month 09 day 20 language R>I don't have 1.7.1 handy at this location to test, but I would've expected a data.frame-like object upon subsetting; should I have expected otherwise? (granted, a data.frame with just a single variable could be thought of as silly, but it does have some extra information that might be worthwhile, on occassion?) I'm not sure that it is a bug, but I was caught by suprise. If it isn't a bug, and someone has a concise way to think through this, for my future reference, I'd appreciate hearing about it. best, -tony -- rossini at u.washington.edu http://www.analytics.washington.edu/ Biomedical and Health Informatics University of Washington Biostatistics, SCHARP/HVTN Fred Hutchinson Cancer Research Center UW (Tu/Th/F): 206-616-7630 FAX=206-543-3461 | Voicemail is unreliable FHCRC (M/W): 206-667-7025 FAX=206-667-4812 | use Email CONFIDENTIALITY NOTICE: This e-mail message and any attachme...{{dropped}}
Have you investigated the drop= argument to "["? (as in the expression testdata[,2,drop=F], which will return a dataframe). "[.data.frame" has somewhat different behavior from "[" on matrices with respect to the drop argument: If the result would be a dataframe with a single column, the default behavior of "[.data.frame" is to return a vector (return a dataframe always if drop=F), but if the result would be a dataframe with a single row, the default behavior is to return a dataframe (return a list if drop=T). E.g.: > class(data.frame(a=1:3,b=4:6)[,1]) [1] "integer" > class(data.frame(a=1:3,b=4:6)[,1,drop=F]) [1] "data.frame" > class(data.frame(a=1:3,b=4:6)[1,]) [1] "data.frame" > class(data.frame(a=1:3,b=4:6)[1,,drop=T]) [1] "list" > The default behavior is often what you want, but when it isn't it can be confusing, especially it's not that easy to find documentation for this (at least not in a quick look through the FAQ, ?"[", and "An Introduction to R" -- please excuse me if I overlooked something.) The thing you have going on with names(testdata[...]) is merely a consequence of whether or not the result of the subsetting operation is a dataframe or a vector. hope this helps, Tony Plate At Tuesday 04:08 PM 9/23/2003 -0700, you wrote:>In playing around with data.frames (and wanting a simple, cheap way to >use the variable and case names in plots; but I've solved that with >some hacks, yech), I noticed the following behavior with subsetting. > > >testdata <- data.frame(matrix(1:20,nrow=4,ncol=5)) >names(testdata) ## expect labels, get them >names(testdata[2,]) ## expect labels, get them >names(testdata[,2]) ## expect labels, but NOT -- STRIPPED OFF?? >testdata[,2] ## would have expect a name (X2) in the front? NOT EXPECTED >testdata[2,] ## get what I expect >testdata[2,2] ## just a number, not a sub-data.frame? unexpected >testdata[2,2:3] ## this is a data.frame >testdata[2:3,2:3] ## and this is, too. > > > version > _ >platform i386-pc-linux-gnu >arch i386 >os linux-gnu >system i386, linux-gnu >status alpha >major 1 >minor 8.0 >year 2003 >month 09 >day 20 >language R > > > >I don't have 1.7.1 handy at this location to test, but I would've >expected a data.frame-like object upon subsetting; should I have >expected otherwise? (granted, a data.frame with just a single >variable could be thought of as silly, but it does have some extra >information that might be worthwhile, on occassion?) > >I'm not sure that it is a bug, but I was caught by suprise. If it >isn't a bug, and someone has a concise way to think through this, for >my future reference, I'd appreciate hearing about it. > >best, >-tony > >-- >rossini at u.washington.edu http://www.analytics.washington.edu/ >Biomedical and Health Informatics University of Washington >Biostatistics, SCHARP/HVTN Fred Hutchinson Cancer Research Center >UW (Tu/Th/F): 206-616-7630 FAX=206-543-3461 | Voicemail is unreliable >FHCRC (M/W): 206-667-7025 FAX=206-667-4812 | use Email > >CONFIDENTIALITY NOTICE: This e-mail message and any attachme...{{dropped}} > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Tue, 2003-09-23 at 18:08, A.J. Rossini wrote:> In playing around with data.frames (and wanting a simple, cheap way to > use the variable and case names in plots; but I've solved that with > some hacks, yech), I noticed the following behavior with subsetting. > > > testdata <- data.frame(matrix(1:20,nrow=4,ncol=5)) > names(testdata) ## expect labels, get them > names(testdata[2,]) ## expect labels, get them > names(testdata[,2]) ## expect labels, but NOT -- STRIPPED OFF?? > testdata[,2] ## would have expect a name (X2) in the front? NOT EXPECTED > testdata[2,] ## get what I expect > testdata[2,2] ## just a number, not a sub-data.frame? unexpected > testdata[2,2:3] ## this is a data.frame > testdata[2:3,2:3] ## and this is, too. > > > version > _ > platform i386-pc-linux-gnu > arch i386 > os linux-gnu > system i386, linux-gnu > status alpha > major 1 > minor 8.0 > year 2003 > month 09 > day 20 > language R > > > > I don't have 1.7.1 handy at this location to test, but I would've > expected a data.frame-like object upon subsetting; should I have > expected otherwise? (granted, a data.frame with just a single > variable could be thought of as silly, but it does have some extra > information that might be worthwhile, on occassion?) > > I'm not sure that it is a bug, but I was caught by suprise. If it > isn't a bug, and someone has a concise way to think through this, for > my future reference, I'd appreciate hearing about it. > > best, > -tonyTony, A quick review of what is returned when you subset the data.frame testdata:> str(testdata[,2])int [1:4] 5 6 7 8> str(testdata[2,])`data.frame': 1 obs. of 5 variables: $ X1: int 2 $ X2: int 6 $ X3: int 10 $ X4: int 14 $ X5: int 18> dim(testdata[,2])NULL> dim(testdata[2,])[1] 1 5 Quoting from ?Extract: "When [.data.frame is used for subsetting rows of a data.frame, it returns a data frame with unique (and non-missing)row names, if necessary transforming the names using make.names( * , unique = TRUE)" What is unstated, but covered by R FAQ 7.7 ("Why do my matrices lose dimensions?"), a single column in a data.frame resulting from the subset operation is by default turned into a vector. Hence, no names. HTH, Marc Schwartz