"[.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
-- [EMAIL PROTECTED] 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}}
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help