by Suf | Sep 4, 2022 | Programming, R, Tips
The easiest way to center a title using ggplot2 is to modify the plot.title component of the theme using element_text(hjust=0.5). For example, library(ggplot2) ggplot(data=mtcars, aes(x=mpg, y=wt)) + geom_point() + ggtitle(“Automobile Weight vs Miles per...
by Suf | Sep 3, 2022 | Programming, R, Tips
The easiest way to transpose a data frame is to use the transpose() function from the data.table library. For example, library(data.table) # get data data(“mtcars”) # transpose t_mtcars <- transpose(mtcars) # get row and colnames in order...
by Suf | Sep 1, 2022 | Programming, R, Tips
Using the rowwise function from dplyr in combination with the mutation function, you can apply a function to every row in a table. For example, data %>% rowwise() %>% # a, b, c are column names mutate(sum_val = sum(a, b, c)) This tutorial will go through how to use...
by Suf | Aug 29, 2022 | Programming, R, Tips
The easiest way to add a sequence of numbers as an index column is to use the nrow() function, for example, df$index <- 1:nrow(df) This tutorial will explain how to add a numeric ID column to a data frame with code examples. Table of contentsAdd Index Column to...
by Suf | Aug 22, 2022 | Programming, R, Tips
The error “plot. new has not been called yet” occurs when you try to do something that requires a plot to exist but have not yet created a plot. You can solve this by creating a plot first before trying to perform the required action. For example, #...