How to Convert Date to Character in R

Convert Date to Character in R

Here are two ways to convert Date to Character in R: Using as.character() Using format() Method 1: Using as.character() dates <- c(“11/20/80”, “11/20/91”, “11/20/1993”, “09/10/93”) dt <- as.Date(dates, “%m/%d/%y”) strDates <- as.character(dt) strDates Output [1] “1980-11-20” “1991-11-20” “2019-11-20” “1993-09-10” Method 2: Using format() If you need to format the date in a specific way during … Read more

R as.factor() Function

as.factor in r

The as.factor() function in R is used to convert an object into a factor, a data type used for categorical variables. When an object is converted to a factor, the unique values in the object become the levels of the factor. Syntax as.factor(input) Parameters input: It takes x as a column in an object of class … Read more

How to Get Current Date and Time in R

Current Date and Time in R

Current Date To get the current date, you can use the “Sys.Date()” function. Sys.Date() Output [1] “2023-05-23″ Current Date and Time To get the current date and time, use the “Sys.time()” function. Sys.time() Output [1] “2023-05-23 22:13:24 IST” To get the system time in a fixed-format character string, use the “date()” function. date() Output [1] … Read more

How to Count NA Values in R

Count NA Values in R

To count NA (missing) values in R, use the is.na() function along with the sum() function. The is.na() function returns a logical vector with the same length as the input object, suggesting whether each element is NA. Then, you can use the sum() function to count the number of TRUE values in the logical vector, representing … Read more

R is.element() Function

R is.element()

The is.element() function is used to check if elements of one object are contained in another object. It’s essentially a more readable version of the %in% operator. This function operates element-wise and returns a logical vector, which makes it suitable for use within indexing brackets []. Syntax is.element(element, data) Parameters element: A vector containing the … Read more

How to Unload a Package in R [3 Ways]

Unload a Package Without Restarting in R

Here are three ways to unload a package in R: Using detach() function with unload argument set to TRUE. Using unloadNamespace() Unloading a package in RStudio Method 1: Using detach() You can unload a package without restarting your session by using the detach() function. This function removes the package from the search path, effectively unloading … Read more

R as.double() and is.double() Functions

as.double() and is.double() Functions in R

as.double() The as.double() function is used to convert an object to double-precision floating point numbers. Syntax as.double(x, …) Parameters x: It is an object to be coerced or tested. …: further arguments passed to or from other methods. Visual representation Example vec <- 1:5 float_vec <- as.double(vec) float_vec typeof(float_vec) Output [1] 1 2 3 4 … Read more

How to Append an Element to Empty List in R [3 Ways]

Append an Element to Empty List in R

Here are three ways to append an element to an empty list in R: Using append() Using numerical indexing Using names Method 1: Using append() Example 1: Appending an element empty_list <- list() element <- list(a = 4) list_with_element <- append(empty_list, element) print(list_with_element) Output $a [1] 4 Example 2: Appending a vector empty_list <- list() … Read more

R While Loop

R While Loop

While loop in R is used to execute a block of code repeatedly as long as a specified condition is TRUE. Syntax while (test_expression) { statement } Here, the test_expression is evaluated, and the loop’s body is entered if the result is TRUE. The statements inside the loop are executed, and the flow returns to re-assess … Read more