WangMuMu
WangMuMu

Reputation: 1

why fill in a matrix with a vector by matrix() is not working as expected?

enter image description here

As shown in the picture, it showed me "numeric 48" instead of actual numbers from the vector. Why is that and how to fix it? Thank you in advance!

Upvotes: 0

Views: 27

Answers (1)

akrun
akrun

Reputation: 886938

The 'x' is a data.frame, so we may need to either convert to matrix with as.matrix

x1 <- as.matrix(x)

Or unlist the data.frame and convert to matrix with new dimensions

x1 <- matrix(unlist(x), 8, 6)

Reproducible example with

data(iris)
x1 <- matrix(iris, 150, 5)
head(x1)
#     [,1]        [,2]        [,3]        [,4]        [,5]       
#[1,] Numeric,150 Numeric,150 Numeric,150 Numeric,150 Numeric,150
#[2,] Numeric,150 Numeric,150 Numeric,150 Numeric,150 Numeric,150
#[3,] Numeric,150 Numeric,150 Numeric,150 Numeric,150 Numeric,150
#[4,] Numeric,150 Numeric,150 Numeric,150 Numeric,150 Numeric,150
#[5,] factor,150  factor,150  factor,150  factor,150  factor,150 
#[6,] Numeric,150 Numeric,150 Numeric,150 Numeric,150 Numeric,150

Upvotes: 0

Related Questions