问题
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