How to Calculate Mean / Average in R

How to Calculate Average in R

To calculate a mean or average in R, you can “use the mean() function.” For example, mean(c(1, 2, 3, 4)) function returns 2.5. Syntax mean(x, trim, na.rm) Parameters x: The x is a Numeric Vector. trim: It drops some observations from both ends of the sorted vector. na.rm: It is a boolean value to ignore the NA … Read more

How to Import CSV Files into R [3 Ways]

How to Import CSV Files into R

Here are three ways to import CSV files into R: Using the read.csv() function from base R Using the read_csv() function from the readr package Using the fread() function from data.table package Method 1: Using the read.csv() function from base R The read_csv() function comes with base R, so no additional libraries are needed, and it … Read more

How to Export DataFrame to CSV in R

How to Export DataFrame to CSV in R using write.csv()

Here are three methods to export DataFrame to CSV in R: Using the write.csv() function from the base package Using the write_csv() function from readr package Using the fwrite() function from data.table package Method 1: Using write.csv() function To export data frame to csv in R, you can “use the write.csv() function.” The write.csv() function “writes … Read more

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