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 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 Summarise Multiple Columns using dplyr in R

Summarise Multiple Columns By Group in R

When we say summarise multiple columns, it means aggregate the input data by applying summary functions (sum, mean, max, etc.) to multiple numeric columns simultaneously. The below image describes visually: If grouping is required, you can group by a specific categorical column and get the statistics for each group. The dplyr package provides the summarise() … Read more