R as.Date() Function: Working with Dates

as.Date() Method in R

The as.Date() function in R converts various types of date and time objects or character strings into Date objects. Syntax as.Date(x, format, tryFormats = c(“%Y-%m-%d”, “%Y/%m/%d”), optional = FALSE, tz = “UTC”, origin, …) Parameters Name Description x It is an object to be converted. format It is a character string. If the format argument … Read more

R pnorm() Function [With Graphical Representation]

R pnorm() Method

The pnorm() function in R calculates the cumulative density function (cdf) value of the normal distribution given a specific random variable q, the population mean μ, and the population standard deviation σ. It returns the probability that a normally distributed random variable is less than or equal to a given value. It provides the probability … Read more

Converting Vector to String in R

Converting vector to string in R

You may want to convert a vector to a string when you need to combine vector elements into a single character string for data export. For example, exporting into csv, text, or database files. Here are three ways to convert a vector to a string in R: Using paste() Using paste0() Using toString() Method 1: … Read more

How to Set and Get Working Directory [setwd() and getwd()] in R

setwd() and getwd() in R

Set the current working directory The setwd() function sets the working directory to the new location, allowing us to change the default location for file operations. The working directory is a location where R looks for files to read or save by default. Use absolute paths for reliability, relative paths for flexibility. Syntax setwd(dir) Parameters … Read more

R dnorm(): Probability Density Function

dnorm() in R

The dnorm() function in R calculates the value of the probability density function (pdf) of the normal distribution of a given value or vector of values. It determines the probability density of a continuous random variable following a normal distribution, characterized by a mean (μ) and standard deviation (σ). How do you define the normal … Read more

R rep() Function: Repeating Elements of a Vector

R rep() Method

R rep() is a generic function that replicates elements of vectors and lists for a specific number of times. The best use case is when working with time-series analysis; you may want to repeat certain values to simulate specific conditions and obtain an accurate output, which is where the rep() function is helpful. Syntax rep(vec, times = 1, … Read more

Splitting Strings: A Beginner’s Guide to strsplit() in R

R strsplit() Method

The strsplit() function in R splits elements of a character vector into a list of substrings based on a specified delimiter or regular expression pattern. In the above figure, we are splitting a character vector “string” into three substrings based on the space in between them. string <- (“Hello dystopian world”) strsplit(string, split = ” … Read more

Understanding of rnorm() Function in R

What is rnorm() function in R

The rnorm() method in R generates random numbers from a normal (Gaussian) distribution, which is characterized by a bell-shaped curve defined by its mean and standard deviation, which are widely used in statistical simulations and data analysis. To understand in simple terms, let me take an example.  Let’s say I want to select some candies. … Read more

as.factor() in R: Converting a Vector to Categorical Data

R as.factor() Function

The as.factor() function in R converts a vector object into a factor. Factors store unique values as levels and are essential for statistical modeling, data visualization, and ensuring the proper handling of categorical data in analyses. The above figure perfectly describes the conversion from vector to factor. The main difference between factor() and as.factor() is … Read more