How to Fix R Error: discrete value supplied to continuous scale

How to Fix R Error discrete value supplied to continuous scale

R Error: discrete value supplied to continuous scale typically occurs when you apply a continuous scale to an axis in ggplot2, yet the variable on that axis is not numeric. The main reason for the error is when you use a scale function intended for continuous data (like scale_x_continuous or scale_y_continuous) on a discrete variable, … Read more

R n_distinct() Function

R n_distinct() Function

The n_distinct() function is used to count the number of unique/distinct combinations in a set of one or more vectors. It’s a faster and more concise equivalent to the nrow(unique(data.frame(…))) function. Syntax n_distinct(…, na.rm = FALSE) Parameters …: Unnamed vectors. If multiple vectors are supplied, then they should have the same length. na.rm: If TRUE, exclude … Read more

How to Recode Values Using recode() Function

How to Recode Values Using recode() Function

To recode values using the dplyr package, use the recode() function. Syntax df %>% mutate(column_name = recode(column_name, old_value1 = new_value1, old_value2 = new_value2, …)) Visual representation Example 1: Recode a Single Column in a Dataframe library(dplyr) df <- data.frame( Age = c(20, 21, 19, 22, 23, 20, 21), Gender = c(“Male”, “Female”, “Male”, “Female”, “Male”, … Read more

How to Calculate Relative Frequencies in R

How to Calculate Relative Frequencies in R

Here are two ways to calculate relative frequencies in R: Using dplyr package Using frequency tables Method 1: Using the dplyr package Count the number of occurrences of each value or group. Calculate the sum of all counts to get the total. Divide each count by the total to get the relative frequency. Example 1: … Read more

How to Select First Row by Group Using dplyr in R

How to Select First Row by Group Using dplyr in R

To select the first row in each group using the dplyr package, combine group_by(), arrange(), and filter() functions. Syntax df %>% group_by(group_variable) %>% arrange(values_variable) %>% filter(row_number()==1) Visual representation Example 1: Selecting the first row by group library(dplyr) df <- data.frame( Age = c(20, 21, 19, 22, 23, 20, 21), Gender = c(“Male”, “Female”, “Male”, “Female”, “Male”, … Read more

How to Find the Maximum Value By Group in R

How to Find the Maximum Value By Group in R

To find the maximum value of a particular variable by group in R, use thegroup_by() and summarise() functions from the dplyr package. Example 1: Finding max value by group library(dplyr) df <- data.frame( Age = c(20, 21, 19, 22, 23, 20, 21), Gender = c(“Male”, “Female”, “Male”, “Female”, “Male”, “Female”, “Male”), Score = c(85, 90, … Read more

How to Count Observations by Group in R

How to Count Observations by Group in R

To count observations by group in R, use the count() function from the dplyr library. This is a convenient way to count the number of observations per group. It’s essentially a faster and more concise version of group_by() + summarise() for this specific task. Syntax library(dplyr) df %>% count(Gender) Example 1: Count observations for a single … Read more

How to Calculate Five Number Summary in R

How to Calculate Five Number Summary in R

Here are three ways to calculate the Five Number Summary in R: Using fivenum() Using  summary() Using quantile() Five Number Summary is a set of descriptive statistics that provides information about a dataset. It consists of the following five values: Minimum: The smallest value in the dataset. First Quartile (Q1): The value that separates the lowest … Read more

How to Change Legend Size in ggplot2

How to Change Legend Size in ggplot2

To change the legend size of ggplot2 in R, use the “theme()” function, where you can control the text size, key size, and other aspects of the legend’s appearance. Syntax ggplot(data, aes(x=x, y=y)) + theme(legend.key.size = unit(1, ‘cm’), #change legend key size legend.key.height = unit(1, ‘cm’), #change legend key height legend.key.width = unit(1, ‘cm’), #change … Read more