Robert Kubrick
Robert Kubrick

Reputation: 8733

Why rbind converts a list of numeric elements to a character matrix?

Why does rbind convert my list of numeric elements to character?

> class(mymatrix.list)
[1] "list"
> class(mymatrix.list[[1]])
[1] "numeric"
> mymatrix.results = do.call(rbind, mymatrix.list)
> class(mymatrix.results)
[1] "matrix"
> class(mymatrix.results[1])
[1] "character"

Upvotes: 5

Views: 4802

Answers (2)

Justin
Justin

Reputation: 43265

the first arguement of rbind is ... and the help file reads:

Arguments:

     ...: vectors or matrices.  These can be given as named arguments.
          Other R objects will be coerced as appropriate: see sections
          ‘Details’ and ‘Value’.  (For the ‘"data.frame"’ method of
          ‘cbind’ these can be further arguments to ‘data.frame’ such
          as ‘stringsAsFactors’.)

and the character conversion is likely due to one of your lists containing a character.

Upvotes: 2

Dason
Dason

Reputation: 61953

Probably because one of the items in your list contains characters?

mymatrix.list <- list()
for(i in 1:10){
  mymatrix.list[[i]] <- rnorm(26)
}

class(mymatrix.list)
# [1] "list"
class(mymatrix.list[[1]])
# [1] "numeric"
mymatrix <- do.call(rbind, mymatrix.list)
class(mymatrix)
# [1] "matrix"
class(mymatrix[1])
# [1] "numeric"

## Add a character vector to your list
mymatrix.list[[11]] <- LETTERS
mymatrix <- do.call(rbind, mymatrix.list)
class(mymatrix)
# [1] "matrix"
class(mymatrix[1])
# [1] "character"

Upvotes: 4

Related Questions