How to Import Excel Files into R

How to Import Excel Files into R

Here are two ways to import Excel files in R: Using read_excel() Using the built-in menu Options of Rstudio Let’s say we have an Excel file like this, and we will import this file in R. Method 1: Using read_excel() The easiest method for reading Excel files in R is using the readxl package’s “read_excel()” … Read more

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

2 Ways to Fix error - discrete value supplied to continuous scale

The error: discrete value supplied to continuous scale typically occurs in R when you use “erroneous use of the scale function along with ggplot().“ library(ggplot2) df <- data.frame(x = 1:10, y = c(“bmw”, “audi”, “mercedez”, “ferrari”, “jaguar”)) ggplot(df, aes(x, y)) + geom_point() + scale_y_continuous(limits = c(0, 10)) Output Error: Discrete value supplied to continuous scale … Read more

What is the n_distinct() Function in R

What is the n_distinct() Function in R

The n_distinct() function in R 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: … Read more

How to Rank Variables by Group Using dplyr in R

Rank Variables by Group Using dplyr in R

To Rank Variables by Group using dplyr in R, you can “combine arrange(), group_by(), or mutate() functions.” Syntax df %>% arrange(group_var, numeric_var) %>% group_by(group_var) %>% mutate(rank = rank(numeric_var)) Example 1: Rank in Ascending Order library(dplyr) df <- data.frame( Age = c(20, 21, 19, 22, 23, 20, 21), Gender = c(“Male”, “Female”, “Male”, “Female”, “Male”, “Female”, … Read more

How to Recode Values Using dplyr in R

How to Recode Values Using dplyr in R

To recode values using dplyr in R, you can “use the recode() function from the dplyr package.” The recode() function from the dplyr package is a handy tool to change or replace values in a column. Syntax df %>% mutate(column_name = recode(column_name, old_value1 = new_value1, old_value2 = new_value2, …)) Example 1: Recode a Single Column … Read more

How to Select the First Row by Group Using dplyr in R

How to Select the First Row by Group Using dplyr in R

To select the first row in each group using the dplyr package in R, you can “combine group_by(), arrange(), and filter()” functions. Syntax df %>% group_by(group_variable) %>% arrange(values_variable) %>% filter(row_number()==1) Example 1: Select the First Row by Group in R library(dplyr) df <- data.frame( Age = c(20, 21, 19, 22, 23, 20, 21), Gender = c(“Male”, “Female”, “Male”, … 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, you can “use the count() function from the dplyr library.” The count() function in the dplyr library is a convenient way to count the number of observations per group. It’s essentially a faster and more concise version of group_by() + summarize() for this specific task. Syntax library(dplyr) df … Read more

How to Create Summary Tables in R

How to Create Summary Tables in R

To create summary tables in R, you can “use the describe() and describeBy() functions from the psych package.” The summary table provides the following information: vars: It represents the column number. n: It represents the number of valid cases. mean: It represents the mean value. median: It represents the median value. min: It represents the … Read more