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

蹲街弑〆低调 提交于 2020-01-04 03:14:08

问题


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"

回答1:


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"



回答2:


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.



来源:https://stackoverflow.com/questions/9518740/why-rbind-converts-a-list-of-numeric-elements-to-a-character-matrix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!