[Rd] data.matrix returns mode logical for zero rows (PR#8496)
ripley@stats.ox.ac.uk
ripley at stats.ox.ac.uk
Wed Jan 18 20:04:24 CET 2006
On Tue, 17 Jan 2006 jonathan.swinton at astrazeneca.com wrote:
> Full_Name: Jonathan Swinton
> Version: 2.2.1
> OS: Windows
> Submission from: (NULL) (193.132.159.169)
>
>
> #The first line of description for data.matrix says that it will
> # 'Return the matrix obtained by converting all the variables in a
> # data frame to numeric mode and then binding them together as the
> # columns of a matrix.'
>
> #However when called with a data.frame with zero rows, data.matrix returns a
> matrix
> #of mode logical rather than numeric. This conflicts with the documentation
> #and is not what seems sensible.
You don't show us an example of a data frame with zero rows, nor do you
show an example of data.matrix returning logical, so this report was very
confusing. Please do give a reproducible example as the posting guide and
FAQ ask.
df[FALSE,] is not a data frame, so I don't know why you expect data.matrix
to be applicable (it is the same as as.matrix in such cases).
Here is an actual reproducible example:
DF <- data.frame(x=c("a", "b"), y=2:3)[FALSE,]
typeof(data.matrix(DF))
> One underlying reason for this is that when a zero-length column of a
> matrix of mode logical is asserted to be numeric the matrix is not
> actually cast to numeric. I wonder if that too is a bug?
No, but there is a bug in your description as you are describing
replacement indexing, not `assertion'. Coercion in replacement indexing
is done to accommodate the new values, and there are none in your example.
(That is how S has always done it in my experience, although the Blue
Book p.359 says otherwise.)
If you want to assert that an object be numeric, use mode(x) <- "numeric".
>> R.version.string
> [1] "R version 2.2.1, 2005-12-20"
>> df <- data.frame(matrix(1:2,nrow=2))
>> mode(data.matrix(df)[,1])
> [1] "numeric"
>> mode(data.matrix(df[FALSE,])[,1])
> [1] "numeric"
>>
>> # Underlying cause
>> x <- matrix(nr = 2,nc = 1 )
>> mode(x)
> [1] "logical"
>> x[, 1] <- c(1,2)
>> mode(x)
> [1] "numeric"
>>
>> x0 <- matrix(nr = 0, nc = 1)
>> x0[, 1] <- numeric(0)
>> mode(x0)
> [1] "logical"
>> mode(x0[,1])
> [1] "logical"
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel
mailing list