Convert DataFrame to Matrix in R

To convert a DataFrame to a Matrix in R using the ‘as.matrix()‘ function: Copy mat <- as.matrix(df) For example: Copy # 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) … Read more

Convert Matrix to DataFrame in R

To convert a Matrix to a DataFrame in R using the as.data.frame() function: Copy 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): Copy … Read more

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): Copy my_matrix <- matrix(data = c(12, 15, 18, 23, 25, 27), nrow = 3, ncol = 2) print(my_matrix) The result: (2) Create a numeric matrix with 3 … Read more

How to Check the Object Type in R

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

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: Copy df$column_name <- trimws(df$column_name) (2) To remove leading and trailing whitespace across the entire DataFrame: Copy df[] <- lapply(df, trimws) Example 1: Remove Whitespace under a Single Column To … Read more

View the Bottom Rows in R DataFrame using Tail

The tail() function can be used to view the bottom rows in R DataFrame: Copy tail(df, number of bottom rows needed) For example, to view the bottom 2 rows: Copy # 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”, … Read more

View the Top Rows in R DataFrame using Head

The head() function can be used to view the top rows in R DataFrame: Copy head(df, number of top rows needed) For example, to view the top 3 rows: Copy # 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”, … Read more

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: Copy column_names <- names(df) (2) Using the colnames() function: Copy column_names <- colnames(df) (3) Using the str() function which shows the DataFrame structure, including the column names: Copy str(df) Examples of Getting the Column Names … Read more

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: Copy # 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 = … Read more

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: Copy # 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 … Read more