2012-03-01 66 views
3

爲什麼rbind會將我的數字元素列表轉換爲字符?爲什麼rbind將數字元素列表轉換爲字符矩陣?

> 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" 
+0

如果您發佈一個最小可重現的示例,它會更容易幫助。 – Andrie 2012-03-01 15:21:26

+1

你應該提供'lapply(mymatrix.list,class)'。 – 2012-03-01 15:40:14

回答

4

可能是因爲您的列表中的某個項目包含字符?

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" 
+0

的確,我的程序中存在一個錯誤。我有一個字符類型列表中的元素。 – 2012-03-01 16:22:33

1

rbind第一arguement是...和幫助文件讀取:

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’.) 

和字符轉換可能是由於包含字符您的列表之一。