Skip to content
Prev 31229 / 63421 Next

(PR#13487) Segfault when mistakenly calling [.data.frame

Simon Urbanek wrote:
The common situation I have (which might be the same as the OP's) is 
wanting to get a vector from a data frame, and having the rownames of 
the dataframe become the names on the vector.

With matrix, the behavior I want is the default behavior, e.g.,
 > x <- cbind(a=c(x=1,y=2,z=3),b=4:6)
 > x
  a b
x 1 4
y 2 5
z 3 6
 > x[,1]
x y z
1 2 3

But with a data frame, subscripting returns a vector with no names:
 > xd <- as.data.frame(x)
 > xd[,1]
[1] 1 2 3

One can use drop=FALSE, but then you've still got a data frame, not a 
vector:
 > (xd1 <- xd[,1,drop=FALSE])
  a
x 1
y 2
z 3

The simplest way I know to get a named vector is to use as.matrix on the 
one-column dataframe:
 > as.matrix(xd1)[,1]
x y z
1 2 3
 >
(Which works fine except in the case where xd1 has only one row...)

And BTW, am I missing something, or does the behavior of drop() not 
conform to the description in ?drop:
 > Value:
 >     If 'x' is an object with a 'dim' attribute (e.g., a matrix or
 >     'array'), then 'drop' returns an object like 'x', but with any
 >     extents of length one removed.  Any accompanying 'dimnames'
 >     attribute is adjusted and returned with 'x': if the result is a
 >     vector the 'names' are taken from the 'dimnames' (if any).  If the
 >     result is a length-one vector, the names are taken from the first
 >     dimension with a dimname.

How is this last sentence consistent with the following behavior?
 > dimnames(x[1,1,drop=F])
[[1]]
[1] "x"

[[2]]
[1] "a"

 > drop(x[1,1,drop=F])
[1] 1
 >
 From the description in "Value:" in ?drop, I would have expected above 
result to have the name "x" (the name from the first dimension with a 
dimname).

 > sessionInfo()
R version 2.8.1 (2008-12-22)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base    
 >

-- Tony Plate