How to declare each column type

Does someone know how to declare the column types of a matrix? I have a matrix with 5 columns, the first one is dates and the others are numeric values. I just found a solution to specify the column types by reading an xlsx. file, but this is a matrix created with different files in R, so I don't want to read an xlsx. file.

A matrix can only store one data type, as far as I know. Typically, you would use a data frame to store multiple data types.

2 Likes

Hi there,

You're looking for a dataframe and not a matrix. A dataframe can have different column types. See the example below.

m <- 
matrix(
c(1:10),
letters[1:10]
)
#> Error in matrix(c(1:10), letters[1:10]): non-numeric matrix extent


df <- 
  data.frame(
    c(1:10),
    letters[1:10]
  )

Created on 2021-09-15 by the reprex package (v2.0.0)

1 Like

Thank you very much for your answer!

Feel free to mark the post that helped you as the solution and or let us know if you any other questions :slight_smile:

1 Like