How to Add an Empty Column to a Dataframe in R (with tibble)

How do I add an empty column in R?

In this short R tutorial, you will learn how to add an empty column to a dataframe in R. Specifically, you will learn 1) to add an empty column using base R, 2) to add an empty column using the add_column function from the package tibble and we are going to use a pipe (from dplyr). Now, dplyr comes with many handy functions that, apart from adding columns, make it easy to remove a column from the R dataframe (e.g., using the select() function). Both tibble and dplyr are part of the tidyverse package. 

The outline of this post is as follows:

  1. Reading Data from an Excel File
  2. How to Add an Empty Column to the Dataframe with:
    • Base R
    • add_column()
  3. How to Add Multiple Empty Columns with:
    • Base R
    • add_column()

Read more

How to Extract Day from Datetime in R with Examples

Learn how to extract days from datetime in R - lubridate - format - readr

In this short R tutorial, you will learn how to extract days from datetime in R. This tutorial will contain a couple of examples containing R code and comments about the code. Extracting days from datetime is a task that we may need to do whenever we are working with a dataset and you want to carry out a time series analysis but only want to include day, for example. It is, of course, possible to use the similar methods as in this post to extract timestamps from datetime, as well.


As R is a versatile statistical programming language, there are many ways to carry out this extraction task.  For instance, we can extract days with the format function. However, first we need to have converted a vector, or a column in a dataframe, to a as.POSIXct class. 

Read more