data.matrix returns mode logical for zero rows (PR#8496)
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. # 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?
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"