[R] Infelicity in print output with matrix indexing of `[.data.frame`

David Winsemius dwinsemius at comcast.net
Sat Dec 17 19:49:45 CET 2016


This puzzle started with an SO posting where the questioner showed output from a dataframe that had been indexed with a matrix. The output appeared to show that numeric values had been coerced to character. Once I got a reproducible example I discovered that the print output was the problem and that the actual values had not been coerced. I've created a much smaller test case and it appears from the testing below that a matrix indexed output from a dataframe with mixed numeric and character types will be printed as character even if none of the values indexed are character:

> dat <- setNames( as.data.frame( matrix(1:12, ncol=4) ), LETTERS[1:4])
> dat
  A B C  D
1 1 4 7 10
2 2 5 8 11
3 3 6 9 12
> dat[2,4]<-NA
> dat[3,3]<-NA
> ng <- which(is.na(dat), arr.ind=TRUE)
> ng
     row col
[1,]   3   3
[2,]   2   4
> dat[ng] <- 20
> dat[ng]
[1] 20 20

That was as expected. Now repeat the process with a dataframe of mixed types.

> dat[2,4]<-NA
> dat[3,3]<-NA
> dat[,1]<- "a"
> dat
  A B  C  D
1 a 4  7 10
2 a 5  8 NA
3 a 6 NA 12
> dat[ng] <- 20
> dat[ng]
[1] "20" "20"

Quoted print output was not what I was expecting.

-- 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list