How to Create an Empty Vector and Append Values in R

Creating an Empty Vector and Append Values in R

R vectors are atomic, meaning they contain homogeneous data types. They are contiguous in memory. Here are some of the best approaches for vector initialization and appending values in the future: Using type-specific functions The most efficient and quickest way to create an empty vector is to use type-specific functions like numeric(0), which will return … Read more

How to Convert Date to Numeric in R

Converting Date to Numeric Value in R

Dates in R are stored as the number of days since 1970-01-01, so converting a Date object to a Numeric gives those day counts. But what about POSIXct? Well, those are stored as seconds since the same epoch, so converting them would give a much larger number. Here are three ways to convert Date to … Read more

How to Create a Data Frame from Vectors in R

Creation of Data Frame from vectors

To create a data frame from vectors in R, use the data.frame() function and pass the vectors. Ensure you have the same length of all the vectors; otherwise, it throws an error. The above figure shows five vectors. Each vector has its own type and the same length, which is five. After converting vectors to … Read more

R dplyr::filter() Function: Complete Guide

R dplyr filter() Function

The dplyr filter() function in R subsets a data frame and retains all rows that satisfy the conditions. In other words, you can select the data frame rows based on conditions. To retain rows, they should produce the output to TRUE, and if they return NA, they will be dropped from the data frame. Syntax … Read more

R distinct() Function from dplyr

R dplyr distinct() Function

The dplyr::distinct() function in R removes duplicate rows from a data frame or tibble and keeps unique rows. You can provide additional arguments, like columns, to check for duplicates in those specific columns. Syntax distinct(.data, …, .keep_all = FALSE) Parameters Argument Description .data It is an input data frame or tibble from which to remove duplicate … Read more

How to Remove NA Values from Data Frame in R

Removing rows with NA in data frame

Here are four different ways for different scenarios to remove NA values from a data frame: Use na.omit() Use complete.cases() Use is.na() with Subsetting Tidyverse approach NA values are missing values. They are somehow absent from a data frame. Before creating a model based on a data frame, we need to clean the data frame … Read more

R append() Function: Complete Guide

append() function in R

The append() function in R concatenates values to a vector or list at a specified position. It returns a new object without modifying the original. You can add a single or multiple elements to a vector or list. Syntax append(obj, values, after = length(x)) Parameters Name Value obj It is an original vector or list. … Read more

How to Remove NULL from List and Nested List in R

Removing NULL from List and Nested List in R

NULL represents a null object, and sometimes, it’s logical for the project to filter it out from either a list or data frame. Here are three ways to remove NULL values from a list in R: Using Filter() with Negate() and is.null() Using sapply() or lapply() with Subsetting Using purrr::discard() (Tidyverse Approach) For example, if … Read more