61

I have a dataframe, df, with a a number of columns of data already. I have a vector, namevector, full of strings. I need empty columns added to df with the names of the columns from namevector.

I am trying to add columns with this for loop, iterating over each string in namevector.

for (i in length(namevector)) {
  df[, i] <- NA
}

but am left with this error:

Error in [<-.data.frame(*tmp*, , i, value = NA) : new columns would leave holes after existing columns

Alternatively, I have thought of creating an empty dataframe with the correct names, then cbind-ing the two dataframes together but am not sure how to go about coding this.

How would I go about resolving this?

6
  • 2
    I think maybe you meant seq_len(length(namevector)) or something similar.
    – joran
    Aug 13, 2013 at 16:33
  • you mean for (i in (seq_len(length(namevector)) + ncol(df))) ... then change the column names. Aug 13, 2013 at 17:03
  • 1
    @JamesPringle seq_len(length(...)) is a longcut to seq_along()
    – Señor O
    Aug 13, 2013 at 17:06
  • @JamesPringle Thanks, I forgot about adding the offset since they're adding new columns.
    – joran
    Aug 13, 2013 at 17:09
  • 1
    @rilkehayden See add_column() Nov 11, 2020 at 17:07

6 Answers 6

85

The problem with your code is in the line:

for(i in length(namevector))

You need to ask yourself: what is length(namevector)? It's one number. So essentially you're saying:

for(i in 11)
df[,i] <- NA

Or more simply:

df[,11] <- NA

That's why you're getting an error. What you want is:

for(i in namevector)
    df[,i] <- NA

Or more simply:

df[,namevector] <- NA
1
  • 9
    Please consider moving df[,namevector] <- NA to the top of your answer, since that is the general solution
    – stevec
    Feb 12, 2021 at 0:13
31
set.seed(1)
example <- data.frame(col1 = rnorm(10, 0, 1), col2 = rnorm(10, 2, 3))
namevector <- c("col3", "col4")
example[ , namevector] <- NA

example
#          col1       col2 col3 col4
# 1  -0.6264538  6.5353435   NA   NA
# 2   0.1836433  3.1695297   NA   NA
# 3  -0.8356286  0.1362783   NA   NA
# 4   1.5952808 -4.6440997   NA   NA
# 5   0.3295078  5.3747928   NA   NA
# 6  -0.8204684  1.8651992   NA   NA
# 7   0.4874291  1.9514292   NA   NA
# 8   0.7383247  4.8315086   NA   NA
# 9   0.5757814  4.4636636   NA   NA
# 10 -0.3053884  3.7817040   NA   NA
3
  • The set.seed(1) doesn't seem strictly necessary. Jan 30, 2015 at 16:12
  • 4
    The set.seed was only added so the example data.frame will be the same if a user runs the code. Technically, the rnorm is not necessary at all.
    – dayne
    Jan 30, 2015 at 19:22
  • 1
    Yes, so the random numbers are the same for everyone runs the code. I see. Jan 30, 2015 at 21:53
16

The below works for me

dataframe[,"newName"] <- NA

Make sure to add "" for new name string.

3
  • The OP has requested to Add empty columns to a dataframe with specified names from a vector. So, he wants to add many columns. Your answer does only add one column and the name is not taken from the variable namevector as requested. Therefore, please review your post or consider to delete it. Thank you.
    – Uwe
    Oct 9, 2018 at 5:35
  • you can always run a loop for list of columns
    – Shubham
    Jan 28, 2019 at 23:38
  • Right but then your answer is identical to the accepted answer which suggests for(i in namevector) df[,i] <- NA
    – Uwe
    Jan 28, 2019 at 23:43
4

I prefer the base R solution, but here is a tidyverse solution. Below uses mutate, but the syntax is the same if you were to use tibble::add_column:

library(dplyr)

new_columns <- c("A", "B", "C")

iris %>% 
  mutate(!!!setNames(rep(NA, length(new_columns)), new_columns))

The splice operator uses the name-value pair from setNames to create the columns.

If you had name-value pairs in a list already then you can simply do:

new_columns <- list(A = NA, B = 1, C = "c")

iris %>% 
  mutate(!!!new_columns)

If the length of each list element is 1 then its value is recycled. Otherwise, it needs to have the same length as the data frame (e.g. list(A = 1:150).

0

A different way to add empty columns to a dataframe.

df <- data.frame(name0 = c(1,2,3))
namevector <- c("name1", "name2", "name3")

sapply(namevector, function(x){df[x] <<- numeric()})
  • The for loop is eliminated with the <<- operator.
  • In many of these examples, the type associated with the new empty column will be a logical; however, by specifying numeric() the type will be numeric. Other types, like double() or character(), could be specified.
-2

Maybe

df <- do.call("cbind", list(df, rep(list(NA),length(namevector))))
colnames(df)[-1*(1:(ncol(df) - length(namevector)))] <- namevector

Not the answer you're looking for? Browse other questions tagged or ask your own question.