Convert DataFrame to Matrix in R

To convert DataFrame to Matrix in R using the ‘as.matrix()‘ function: mat <- as.matrix(df) For example: # Create a DataFrame df <- data.frame( col_a = c(1, 2, 3), col_b = c(4, 5, 6), col_c = c(7, 8, 9) ) # Convert the DataFrame to a Matrix in R mat <- as.matrix(df) # Print the Matrix … Read more

Categories R

Convert Matrix to DataFrame in R

To convert a Matrix to a DataFrame in R using the as.data.frame() function: df <- as.data.frame(my_matrix) Example 1: Convert Matrix to DataFrame Here we convert a matrix with 3 rows and 2 columns to a DataFrame (note that the class function was used to check the object type before and after the conversion): # Create … Read more

Categories R

How to Create a Matrix in R

The matrix() function can be used to create a matrix in R: (1) Create a numeric matrix with 3 rows and 2 columns – filled by columns (default): my_matrix <- matrix(data = c(12, 15, 18, 23, 25, 27), nrow = 3, ncol = 2) print(my_matrix) The result: [,1] [,2] [1,] 12 23 [2,] 15 25 … Read more

Categories R

How to Check the Object Type in R

To check the object type in R using the class() function: (1) Numeric: x <- 7 class(x) Output: numeric (2) Character: y <- “Hello world” class(y) Output: character (3) Logical: z <- TRUE class(z) Output: logical (4) Date: d <- as.Date(“2024-02-16”) class(d) Output: Date (5) Integer: i <- as.integer(7) class(i) Output: integer (6) Factor: f … Read more

Categories R

Remove Leading and Trailing Whitespace in R DataFrame

The trimws() function can be used to remove leading and trailing whitespace in R DataFrame: (1) To remove leading and trailing whitespace under a single column: df$column_name <- trimws(df$column_name) (2) To remove leading and trailing whitespace across the entire DataFrame: df[] <- lapply(df, trimws) Example 1: Remove Whitespace under a Single Column To remove leading … Read more

Categories R

View the Bottom Rows in R DataFrame using Tail

The tail() function can be used to view the bottom rows in R DataFrame: tail(df, number of bottom rows needed) For example, to view the bottom 2 rows: # Create a sample DataFrame df <- data.frame(product = c(“computer”, “monitor”, “keyboard”, “printer”, “tablet”), price = c(1200, 400, 100, 200, 300), brand = c(“A”, “B”, “C”, “X”, … Read more

Categories R

View the Top Rows in R DataFrame using Head

The head() function can be used to view the top rows in R DataFrame: head(df, number of top rows needed) For example, to view the top 3 rows: # Create a sample DataFrame df <- data.frame(product = c(“computer”, “monitor”, “keyboard”, “printer”, “tablet”), price = c(1200, 400, 100, 200, 300), brand = c(“A”, “B”, “C”, “X”, … Read more

Categories R

Get all the Column Names in a DataFrame in R

Here are 3 ways to retrieve all the column names in a DataFrame in R: (1) Using the names() function: column_names <- names(df) (2) Using the colnames() function: column_names <- colnames(df) (3) Using the str() function which shows the DataFrame structure, including the column names: str(df) Examples of Getting the Column Names in a DataFrame … Read more

Categories R

How to Create a Line Chart in R

Here are 2 ways to create a line chart in R: (1) Using the base R plotting functions: # Sample data x <- c(1, 3, 5, 7, 9, 11, 13, 15, 17, 19) y <- c(2, 4, 6, 9, 12, 15, 16, 13, 7, 5) # Build the line chart plot(x, y, type = “l”, … Read more

Categories R

How to Create a Pie Chart in R

Here are two ways to create a pie chart in R: (1) Using the base pie() function in R: # Sample data values <- c(15, 25, 40) labels <- c(“X”, “Y”, “Z”) # Build the pie chart pie(values, labels = labels, main = “My Pie Chart”) (2) Using the ggplot2 package: First install the ggplot2 … Read more

Categories R