Рет қаралды 7,889
How to transform data frames to matrices in the R programming language. More details: statisticsglob...
R code of this video:
my_df <- data.frame(x1 = 3:8, # Create data frame
x2 = 10:5,
x3 = 4)
my_df # Print data frame
class(my_df) # Class of data
my_mat <- as.matrix(my_df) # Convert data frame to matrix
my_mat # Print matrix
class(my_mat) # Class of data
my_df_fac <- my_df # Duplicate data
my_df_fac$x2 <- as.factor(my_df_fac$x2) # Convert column to factor
my_mat_fac <- as.matrix(my_df_fac) # Using as.matrix function
my_mat_fac # Print character matrix
my_mat_num <- apply(as.matrix.noquote(my_df_fac), # Using apply function
2,
as.numeric)
my_mat_num # Print numeric matrix
Follow me on Social Media:
Facebook: / statisticsglobecom
LinkedIn: / statisticsglobe
Patreon: / statisticsglobe
Pinterest: www.pinterest....
Reddit: / joachimschork
Twitter: / joachimschork