Printing an Output of a Program in R

Printing output in R

When working with R in an interactive mode, you don’t need to use any functions or methods to print the result of your expressions; it will print them automatically. Simply typing a variable or expression will print its value. data <- 42 data # [1] 42 However, you need functions to see the output if … Read more

How to Calculate Variance in R

var() function in R

To calculate the sample variance (measurement of spreading) in R, you should use the built-in var() function. It calculates the sample variance (using n−1 for unbiased estimation, where n is the sample size). By default, it does not calculate the population variance. Variance measures how spread out a set of numbers is around the mean … Read more

tryCatch() Function in R

tryCatch() function in R

The tryCatch() function acts as a mechanism for handling errors and other conditions (like warnings or messages) that arise during code execution in R. It allows you to define custom behaviors when an error occurs instead of stopping the execution of the script. The tryCatch() function lets you define custom handlers for four conditions. Each … Read more

R grep(): Finding a Position of Matched Pattern

R grep() Function with various scenarios

The grep() function in R  searches for matches to a pattern within a character vector. It returns indices or values of elements that match the pattern. It is part of a family of functions that includes grepl(), regexpr(), gregexpr(), sub(), and gsub(). Syntax grep( pattern, char, ignore.case = FALSE, value = FALSE, invert = FALSE, perl … Read more

How to Check Data type of a Variable in R

Checking the data type of a variable in R

When it comes to checking the data type of a variable, it depends on what specific thing you are looking for in a variable, because you have many options to choose from. Each option tells you a different but important story. There are four main core functions to check data types in R: typeof(): It … Read more

Mastering grepl() Function in R

grepl() function in R

The grepl() function (stands for “grep logical”) in R searches for patterns within each element of a character vector. It returns a logical vector of the same length as input, where each value is either TRUE or FALSE. If the pattern matches that specific element, it returns TRUE. Otherwise, it returns FALSE. The above figure … Read more

How to Create Directory and File If It doesn’t Exist in R

Check and Create If a Directory and File Exists in R

When working with file systems, checking the directory or file existence is always better before commencing the operations. For a directory To create a directory, if it does not exist, use the dir.exists() function. After checking its existence, if it does not exist, you can use the dir.create() function to create a new directory with a specified … Read more

How to Create a Grouped Boxplot in R

R grouped boxplot with code example

To create a grouped boxplot in R, we can use the ggplot2 library’s aes() and geom_boxplot() functions. The aes() function maps the continuous and categorical variables to visual properties of a plot. The geom_boxplot() function creates a boxplot. There is a difference between a standard boxplot and a grouped boxplot. A standard boxplot provides a … Read more