On 22/09/2016 4:45 AM, Colin Phillips wrote:
I'm sure I'm doing something wrong, but I'm seeing strange behaviour using the 
`head` and `tail` functions on an empty data.frame.

To reproduce:
# create an empty data frame.  I actually read an empty table from Excel using 
`readWorkbook` from package `openxlsx`
test <- structure(list(Code = NULL, Name = NULL, Address = NULL, Sun.Hrs = NULL,
    Mon.Hrs = NULL), .Names = c("Code", "Name", "Address", "Sun.Hrs",
"Mon.Hrs"), class = "data.frame", row.names = integer(0))

That's not a valid dataframe, it's just labelled as one. If you tried to create it with data.frame(), you'd get something different.

> test <- data.frame(Code = NULL, Name = NULL, Address = NULL, Sun.Hrs = NULL,
+     Mon.Hrs = NULL)
> test
data frame with 0 columns and 0 rows

You can create a zero-row dataframe as long as you put 0-length vectors in as columns. NULL is not a vector.

> test <- data.frame(Code = numeric(0), Name = numeric(0), Address = numeric(0), Sun.Hrs = numeric(0),
+     Mon.Hrs = numeric(0))
> test
[1] Code    Name    Address Sun.Hrs Mon.Hrs
<0 rows> (or 0-length row.names)

If you do that, head() works:

> head(test)
[1] Code    Name    Address Sun.Hrs Mon.Hrs
<0 rows> (or 0-length row.names)

So this is a bug in openxlsx. It's also a well-known limitation of the S3 object system: you can easily create things that are labelled with a certain class, but aren't valid objects of that class.

Duncan Murdoch

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to