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

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

as.numeric(): Converting to Numeric Values in R

R as.numeric() Method

The as.numeric() function in R converts valid non-numeric data into numeric data. What do I mean by valid non-numeric data? They are valid numeric values that are stored as text or categorical variables (factors). For example, “1” is a valid non-numeric data but “A” is not valid non-numeric data as far as the as.numeric() method … Read more

Dollar Sign ($ Operator) in R

Dollar operator ($) in R

In R, you can use the dollar sign ($ operator)  to access elements (columns) of a list or a data frame by their name. It also works with your environment. For example, it can fetch you the current environment variables. With its help, you can add, modify, or delete variables from the list and data … Read more

Calculating Absolute Value using abs() Function in R

R abs() Function

The abs() function calculates the absolute value of a numeric input, returning a non-negative (only positive) value by removing any sign. Usage of absolute value is high in fields like mathematical computations (mean absolute errors), statistical analysis, and data cleaning in ML tasks. Syntax abs(input) Parameters Argument Description input It defines the input object. It … 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

R head() and tail() Functions

head() and tail() in R

For data exploration, quickly inspecting the data, verifying the structure, or debugging large datasets, we need some kind of tool that helps prevent excessive data flooding in the console. That’s where head() and tail() generic functions come into play. head() function The head() function displays the first n parts of the vector, matrix, table, or … Read more

Detailed Guide on %in% Operator in R

IN operator in R

The %in% operator checks if elements of one vector are present in another vector. It returns a logical vector of the same length as the left-side argument. Each element in the output is either TRUE or FALSE, depending on the value matching. If the value matches, it returns TRUE; otherwise, it returns FALSE. If you apply … Read more