How to Use extract() Function in R

How to Use extract() Function in R

The extract() function from the tidyr package in R is “used to extract multiple values from a single column into multiple columns.” Syntax extract(data, col, into, regex, remove = TRUE, convert = FALSE) Parameters data: The data frame. col: The column name you want to extract values from. into: A vector of column names you … Read more

How to Use separate() Function in R

How to Use separate() Function in R

The separate() function from the tidyr package in R is “used to separate a single data frame or table column into multiple columns.” Syntax separate( df, col, into, sep = “[^[:alnum:]]+”, remove = TRUE, convert = FALSE ) Parameters df: It is the data frame. column: It is a column that is to be separated. … Read more

How to Use spread() Function in R

How to Use spread() Function in R

The spread() function from the tidyr package in R is “used to spread a key-value pair across different columns.” The spread() function also helps reshape the data from a long to a wide format. Syntax spread(df, key, value) Parameters df: It is the DataFrame. key: It is the column whose values will become the names … Read more

What is the unite() Function in R

What is the unite() Function in R

The unite() function in R is “used to merge two or more columns into a single column or variable.” Syntax unite(data, col, …, sep = “,”, remove = TRUE) Parameters data: It is a table or data frame. col: Name of a new column that is to be added. …: Names of columns that are … Read more

How to Calculate Standard deviation in R

How to Calculate Standard Deviation in R using sd()

Standard deviation is “a statistic that measures the dispersion of a dataset relative to its mean and is calculated as the square root of the variance”. To calculate the standard deviation in R, you can use the “sd()” function. The sd() is a built-in function that “accepts the input object and calculates the standard deviation … Read more

How to Convert a Character to a Timestamp in R

How to Convert a Character to a Timestamp in R

To convert a character to a timestamp in R, you can “use the strptime() function.” This function allows you to specify the format of the date-time string so that it can be converted into a POSIXlt object, another type of date-time object in R. Syntax strptime(character, format = “%Y-%m-%d %H:%M:%S”) Parameters character: The name of the character … Read more

How to Convert Character to Factor in R

How to Convert Character to Factor in R

To convert a character to a factor in R, you can “use the as.factor() function.” Syntax as.factor(char) Parameters char: It is the character vector. Example # Create a character vector fruits <- c(“Apple”, “Banana”, “Cherry”, “Apple”, “Banana”, “Dragonfruit”) # Convert it to a factor fruits_factor <- factor(fruits) # Display the factor print(fruits_factor) Output [1] Apple … Read more

How to Convert Factor to Character in R

How to Convert Factor to Character in R

Here are the ways to convert factor to character in R: Convert a single factor vector to a character vector Convert factor vector columns to character vector columns in a data frame Convert factor vector columns to character vector columns in a data frame where columns are unknown Convert all columns of the Data frame … Read more

How to Import CSV Files into R [3 Ways]

How to Import CSV Files into R

Here are three ways to import CSV files into R: Using the read.csv() function from base R Using the read_csv() function from the readr package Using the fread() function from data.table package Method 1: Using the read.csv() function from base R The read_csv() function comes with base R, so no additional libraries are needed, and it … Read more

How to Export DataFrame to CSV in R

How to Export DataFrame to CSV in R using write.csv()

Here are three methods to export DataFrame to CSV in R: Using the write.csv() function from the base package Using the write_csv() function from readr package Using the fwrite() function from data.table package Method 1: Using write.csv() function To export data frame to csv in R, you can “use the write.csv() function.” The write.csv() function “writes … Read more