R agrep() Function

R agrep() Function

The agrep() function in R is used for approximate string matching, meaning it searches for patterns in a string that approximately match the given pattern, allowing for a certain level of inexactness. Syntax agrep(pattern, x, max.distance = 0.1, ignore.case = FALSE, value = FALSE) Parameters pattern: A specified pattern that will be matched with given … Read more

R col() Function

R col() Function

The col() function in R is used to generate a matrix of integers suggesting the column indices of each element in a matrix. Syntax col(x, dim, as.factor = FALSE) Parameters x: It is a matrix-like object with a two-dimensional dim. dim: It is a matrix dimension, i.e., an integer-valued numeric vector of length two (non-negative … Read more

R dirname() Function

R dirname() Function

The dirname() function in R is used to extract the path to the directory from a full file path. Essentially, it returns the directory part of a file path, excluding the file name. This function is often used in conjunction with other file-handling functions like basename() (which extracts the file name) and file.path() (for constructing … Read more

How to Add Column to Data Frame in R

How to Add Column to Data Frame in R

Here are the five ways to add a column to a data frame in R: Using $ operator Using square ([ ]) notation Using cbind() Using add_column() from the “tidyverse” package Using mutate() from “dplyr” package Method 1: Using the $ operator df <- data.frame( col1 = c(1, 2, 3), col2 = c(4, 5, 6), col3 … Read more

R structure() Function

R structure() Function

The structure() function is used to create or modify the attributes of an R object. You can assign properties like names, dimensions, classes, or other user-defined attributes to an object using this function. Syntax structure(.Data, …) Parameters .Data: an object that will have various attributes attached to it. …: attributes, specified in tag = value … Read more

R setdiff() Function

R setdiff() Function

The setdiff() function in R is used to find the elements present in the first object but not in the second object. It essentially performs a set difference operation between two vectors or similar objects. Syntax setdiff(x, y) Parameters x: It is either a vector or data frame. y: It is either a vector or data frame. … Read more

R read.xlsx() Function

R read.xlsx() Function

The read.xlsx() function in R is used for reading data from Excel files (both .xls and .xlsx formats). It is part of the xlsx package, which can be installed from CRAN. To use the read.xlsx() function, you need to install the “openxlsx“ package. install.package(“openxlsx”) Syntax read.xlsx( xlsxFile, sheet = 1, startRow = 1, colNames = … Read more

How to Calculate Square Root in R

How to Calculate Square Root in R

To calculate the square root of a number, vector, matrix, or list in R, use the sqrt() function. For example, sqrt(16) returns 4, and sqrt(c(1, 4, 9)) returns c(1, 2, 3). Visual Representation Figure 1: Square root of a vector Syntax sqrt(num) Parameters num: It’s a numerical value greater than 0. Return value It returns the square … Read more

R trunc() Function

R trunc() Function

The trunc() function in R is used to remove the fractional part of the value, effectively rounding it down towards zero.  This function acts as a “ceiling()” function for negative numbers and a “floor()” function for positive numbers. For example, trunc(3.14) returns 3 and trunc(-2.7) returns -2. Syntax trunc(data) Parameters data: It takes data as an … Read more