add blank column in R, create empty column in R data frame

How to create a blank column in the R data frame?

Sometimes it is necessary to create a blank column in the R data frame to ensure that it will be available at all times. Here are multiple scenarios on how to do that. It might be just one blank column or multiple blank columns. Sometimes based on which of the necessary columns are missing.

 

Meanwhile, the same principles apply to adding one or multiple columns to the data frame with the same value.

 

Create one blank column in the R data frame

Here is a simple approach to add an empty column in R that comes from this post.

iris$EmptyColumn <- NA

head(iris)

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species EmptyColumn
#1          5.1         3.5          1.4         0.2  setosa          NA
#2          4.9         3.0          1.4         0.2  setosa          NA
#3          4.7         3.2          1.3         0.2  setosa          NA
#4          4.6         3.1          1.5         0.2  setosa          NA
#5          5.0         3.6          1.4         0.2  setosa          NA
#6          5.4         3.9          1.7         0.4  setosa          NA

 

Add a blank column by using dplyr

Here is how to do the same as previously by using dplyr capabilities.

data(iris)

require(dplyr)

iris %>% mutate("EmptyColumn" = NA) %>% head()

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species EmptyColumn
#1          5.1         3.5          1.4         0.2  setosa          NA
#2          4.9         3.0          1.4         0.2  setosa          NA
#3          4.7         3.2          1.3         0.2  setosa          NA
#4          4.6         3.1          1.5         0.2  setosa          NA
#5          5.0         3.6          1.4         0.2  setosa          NA
#6          5.4         3.9          1.7         0.4  setosa          NA

By using the function mutate, you can create and move the column to a certain position in the data frame. Take a look at this post to learn more.

 

Create multiple empty columns in the R data frame

If you have column names, you can add them as multiple empty columns to a data frame like this.

data(iris)

new_columns <- c("FirstEmpty", "SecondEmpty")

iris[, new_columns] <- NA

head(iris)

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species FirstEmpty SecondEmpty
#1          5.1         3.5          1.4         0.2  setosa         NA          NA
#2          4.9         3.0          1.4         0.2  setosa         NA          NA
#3          4.7         3.2          1.3         0.2  setosa         NA          NA
#4          4.6         3.1          1.5         0.2  setosa         NA          NA
#5          5.0         3.6          1.4         0.2  setosa         NA          NA
#6          5.4         3.9          1.7         0.4  setosa         NA          NA

It might be necessary to add the column to the data frame if it does not exist. Here is a simple way how to detect which of the columns are missing, but here is more about that.

data(iris)

new_columns <- c("Species", "FirstEmpty", "SecondEmpty")

x <- subset(new_columns, 
            is.na(
              match(new_columns, names(iris))
            )
)


x

#[1] "FirstEmpty"  "SecondEmpty"

By knowing what is missing, add the column to the R data frame if it does not exist.

iris[, x] <- NA

head(iris)

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species FirstEmpty SecondEmpty
#1          5.1         3.5          1.4         0.2  setosa         NA          NA
#2          4.9         3.0          1.4         0.2  setosa         NA          NA
#3          4.7         3.2          1.3         0.2  setosa         NA          NA
#4          4.6         3.1          1.5         0.2  setosa         NA          NA
#5          5.0         3.6          1.4         0.2  setosa         NA          NA
#6          5.4         3.9          1.7         0.4  setosa         NA          NA

 

Add multiple blank columns by using dplyr

If you want to add a few blank columns by using the function mutate you can do that like this.

iris %>% mutate("FirstEmpty" = NA,
                "SecondEmpty" = NA)

But when there are too many to add, but you want to use dplyr to do that, you can create an empty data frame and bind that together with the existing one.

data(iris)

bind_cols(iris
          , data.frame(matrix(ncol = 3, nrow = nrow(iris))) %>% 
            setNames(c("one", "two", "three"))
          ) %>%
  head()

#  Sepal.Length Sepal.Width Petal.Length Petal.Width Species one two three
#1          5.1         3.5          1.4         0.2  setosa  NA  NA    NA
#2          4.9         3.0          1.4         0.2  setosa  NA  NA    NA
#3          4.7         3.2          1.3         0.2  setosa  NA  NA    NA
#4          4.6         3.1          1.5         0.2  setosa  NA  NA    NA
#5          5.0         3.6          1.4         0.2  setosa  NA  NA    NA
#6          5.4         3.9          1.7         0.4  setosa  NA  NA    NA

 

By the way, here is how to quickly drop an unnecessary column in the R data frame.


Posted

in

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *