Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a object class "matrix" "array" with dataframe

I have a df dataset.

# My data
df <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/refs/heads/main/Dados_C3_data_planta.csv", sep=";")
head(df)

#        Data GLY_PN CRO_P MIPU_P CEN_P POA01_P CECRO_P COM_P MOR_PN
#1 24-01-2024     53    40      3     0       0       0     0      0
#2 25-01-2024     56     8      0    24       9       0     0      0
#3 26-01-2024      0    27      0    47      26       0     0      0
#4 27-01-2024      0     0      0     0       0       6    43     48
#5 31-01-2024      0     0      0    62      35       0     0      0
#6 02-02-2024      0     0      0    50      50       0     0      0
#  COPA_PN RHY_P SOR_P
#1       0     0     0
#2       0     0     0
#3       0     0     0
#4       0     0     0
#5       0     0     0
#6       0     0     0

I’d like to convert the data.frame in class "matrix" "array", but doesn’t work with as.matrix or data.matrix functions.
My desirable output (df_to_matrix) is:

#                GLY_PN CRO_P MIPU_P CEN_P POA01_P CECRO_P COM_P MOR_PN
#1 24-01-2024     53    40      3     0       0       0     0      0
#2 25-01-2024     56     8      0    24       9       0     0      0
#3 26-01-2024      0    27      0    47      26       0     0      0
#4 27-01-2024      0     0      0     0       0       6    43     48
#5 31-01-2024      0     0      0    62      35       0     0      0
#6 02-02-2024      0     0      0    50      50       0     0      0
#  COPA_PN RHY_P SOR_P
#1       0     0     0
#2       0     0     0
#3       0     0     0
#4       0     0     0
#5       0     0     0
#6       0     0     0

class(df_to_matrix)
[1] "matrix" "array"

No column name in the first column and values as numeric. Please any help with it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

# Load the data
df <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/refs/heads/main/Dados_C3_data_planta.csv", sep=";")

# Convert to matrix, excluding the first column (dates)
df_to_matrix <- as.matrix(df[, -1])  # Exclude the first column

# Optionally, convert character columns to numeric
df_to_matrix <- apply(df_to_matrix, 2, as.numeric)

# Check the class
class(df_to_matrix)

# Print the resulting matrix
print(df_to_matrix)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading