How to Add Column to Data Frame in R

Add Column to Data Frame in R

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

How to Use structure() Function in R

structure() Function in R

The structure() function in R is used to describe a given object with given attributes. Syntax structure(.Data, …) Parameters .Data: an object that will have various attributes attached to it. …: attributes, specified in tag = value form, will be attached to data. Example 1: Usage of structure() function mat <- structure(1:6, dim = 2:3) … Read more

How to Use setdiff() Function in R

setdiff() Function in R

The setdiff() function in R is used to find differences between two sets. For example, if x = c(1, 2, 3) and y = c(2, 4), setdiff(x, y) function returns c(1, 3). Syntax setdiff(x, y) Parameters x: It is either a vector or data frame. y: It is either a vector or data frame. Return value It returns … Read more

How to Use read.xlsx() Function in R

read.xlsx() Function in R

The read.xlsx() function in R is used to read data from an Excel file into a data frame. 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

Calculate Square Root in R

To calculate the square root of a number, vector, matrix, or list in R, you can 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 The figure shows that we are finding the square root of the rv vector, which … Read more

How to Use trunc() Function in R

trunc() Function in R

The trunc() function in R is used to truncate the values in the decimal places. Truncation means removing the decimal places of a number without rounding. For example, trunc(3.14) returns 3 and trunc(-2.7) returns -2. It acts as a “ceiling()” function for negative numbers and a “floor()” function for positive numbers. Syntax trunc(data) Parameters data: … Read more

How to Use basename() Function in R

basename() Function in R

The basename() function in R is used to remove all paths up to and including the last path separator (if any) from a given path name. Syntax basename(path) Parameters path: It is a character vector containing path names. Example Let’s define a file path of the current working directory + file name and pass this … Read more

How to Use aggregate() Function in R

aggregate() Function in R

The aggregate() function in R is used to get the summary statistics of the data by the group. Syntax aggregate(x, by, FUN) Parameters x: It is an R object. by: It is a list of grouping items, each as long as the variables in the data frame x. FUN: It is a function to compute the summary … Read more

How to Use fromJSON() Function in R

fromJSON() Function in R

The fromJSON() function is used to convert a JSON into an R object. It reads the content in JSON format and de-serializes it into logical, integers, real numbers, strings, arrays, and lists. Syntax fromJSON( json_str, file, method = “C”, unexpected.escape = “error”, simplify = TRUE ) Parameters json_str: It is a JSON object to convert. file: It … Read more