Q:

Q: Distinction between levels() and unique() for zero-length strings in data frame

If a column in a data frame contains a zero-length string, passing it to the levels() function will return the zero-length string.  However, unique() will not.  This seems counter-intuitive to me, and I haven't been able to find any documentation that explains this.  Does anyone know why this is the case?
Example:
d <- data.frame( col1 = c("", 'a', 'b'))  # Contains "".

Call unique():
unique(d$col1)

unique() does not return the zero-length string element:
[1]   a b
Levels:  a b  

levels() includes "" in the results:
levels(d$col1)
[1] ""  "a" "b"

A:

This is to do with printing methods. From the documentation, the return value for unique:

For a vector, an object of the same type of x, but with only one copy of each duplicated element. No attributes are copied (so the result has no names).

And indeed, you can see that unique return a factor because col1 is also a factor:
class(unique(d$col1))
[1] "factor"

Whereas, the return value for levels is a character vector:
class(levels(d$col1))
[1] "character"

And thus, unique does print the empty string but without the double quotes. An alternative would be to define the df without factor columns:
d <- data.frame(col1 = c("", 'a', 'b'), stringsAsFactors = F)

And here unique will indeed return the wanted "". Of course levels only applies to factors and would no longer be adequate:
unique(d$col1)
[1] ""  "a" "b"

