Why is.vector on a data-frame doesn't return TRUE?

r

Solution

Illustrating what @joran pointed out, that `is.vector` returns false on a vector which has any attributes other than names (I never knew that) ...

# 1) Example of when a vector stops being a vector...
> dubious = 7:11
> attributes(dubious)
NULL
> is.vector(dubious)
[1] TRUE

#now assign some additional attributes        
> attributes(dubious) <- list(a = 1:5)
> attributes(dubious)
$a
[1] 1 2 3 4 5

> is.vector(dubious)
[1] FALSE


# 2) Example of how to strip a dataframe of attributes so it looks like a true vector ...

> df = data.frame()
> attributes(df)
$names
character(0)

$row.names
integer(0)

$class
[1] "data.frame"

> attributes(df)[['row.names']] <- NULL
> attributes(df)[['class']] <- NULL
> attributes(df)
$names
character(0)

> is.vector(df)
[1] TRUE

Problem

tl;dr - What the hell is a vector in R? Long version: Lots of stuff is a vector in R. For instance, a number is a numeric vector of length 1: ``` is.vector(1) [1] TRUE ``` A list is also a vector. ``` is.vector(list(1)) [1] TRUE ``` OK, so a list is a vector. And a data frame is a list, apparently. ``` is.list(data.frame(x=1)) [1] TRUE ``` But, (seemingly violating the transitive property), a data frame is not a vector, even though a dataframe is a list, and a list is a vector. EDIT: It is a vector, it just has additional attributes, which leads to this behavior. See accepted answer below. ``` is.vector(data.frame(x=1)) [1] FALSE ``` How can this be?

Original source

Related problems