7

I want to add multiple empty columns to a tibble. The names of the new columns are stored in 'columnsToAdd'

> columnsToAdd
[1] "column1" "column2" "column3" "column4" "column5"

When I run the following code lines, ...

library(dplyr)

someTibble <- tibble(name = paste("Name", 1:10))

columnsToAdd <- paste("column", 1:30, sep = "")

someTibble %>% 
tibble::add_column(columnsToAdd = NA)

... I get this result, ...

# A tibble: 10 x 2
   name    columnsToAdd
   <chr>   <lgl>       
 1 Name 1  NA          
 2 Name 2  NA          
 3 Name 3  NA          
 4 Name 4  NA          
 5 Name 5  NA          
 6 Name 6  NA          
 7 Name 7  NA          
 8 Name 8  NA          
 9 Name 9  NA          
10 Name 10 NA    

... instead, I want to get the following result:

# A tibble: 10 x 6
   name    column1 column2 column3 column4 column5
   <chr>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
 1 Name 1       NA      NA      NA      NA      NA
 2 Name 2       NA      NA      NA      NA      NA
 3 Name 3       NA      NA      NA      NA      NA
 4 Name 4       NA      NA      NA      NA      NA
 5 Name 5       NA      NA      NA      NA      NA
 6 Name 6       NA      NA      NA      NA      NA
 7 Name 7       NA      NA      NA      NA      NA
 8 Name 8       NA      NA      NA      NA      NA
 9 Name 9       NA      NA      NA      NA      NA
10 Name 10      NA      NA      NA      NA      NA

3 Answers 3

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

20

Just create them as columns. This works for base data frames and data.table tables:

Eg:

> someTibble <- tibble(name = paste("Name", 1:10))
> columnsToAdd <- paste("column", 1:3,sep="")

That gives me one column:

> head(someTibble)
# A tibble: 6 x 1
  name  
  <chr> 
1 Name 1
2 Name 2
3 Name 3
4 Name 4
5 Name 5
6 Name 6

Then I do:

> someTibble[,columnsToAdd]=NA

and magically the columns appear:

> head(someTibble)
# A tibble: 6 x 4
  name   column1 column2 column3
  <chr>  <lgl>   <lgl>   <lgl>  
1 Name 1 NA      NA      NA     
2 Name 2 NA      NA      NA     
3 Name 3 NA      NA      NA     
4 Name 4 NA      NA      NA     
5 Name 5 NA      NA      NA     
6 Name 6 NA      NA      NA     

Note this is not really magic, it is standard base R behaviour since from before R was born.

2
  • 11
    +1 people frequently get so hooked on dplyr that they forget that very often other methods are sooo much simpler. This is a good example. Learning base R should be a required prerequisite before learning dplyr or other packages.
    – dww
    Feb 14, 2019 at 15:39
  • 1
    Always nice to see the simpler answers come through. This answer should also be the correct answer.
    – MrGumble
    Feb 15, 2019 at 6:51
8

We can pass the column names as a list then use the !!! "triple bang"

columnsToAdd <- paste("column", 1:5, sep = "")

someTibble %>% 
     tibble::add_column(!!!set_names(as.list(rep(NA, length(columnsToAdd))),nm=columnsToAdd))

# A tibble: 6 x 6
name   column1 column2 column3 column4 column5
<chr>  <lgl>   <lgl>   <lgl>   <lgl>   <lgl>  
1 Name 1 NA      NA      NA      NA      NA     
2 Name 2 NA      NA      NA      NA      NA     
3 Name 3 NA      NA      NA      NA      NA     
4 Name 4 NA      NA      NA      NA      NA     
5 Name 5 NA      NA      NA      NA      NA     
6 Name 6 NA      NA      NA      NA      NA

However, I think @MrGumble may miss something as from add_column {tibble}

... Name-value pairs, passed on to tibble(). All values must have one element for each row in the data frame, or be of length 1. These arguments are passed on to tibble(), and therefore also support unquote via !! and unquote-splice via !!!.

and here is an example from tibble {tibble}

tibble(!!! list(x = rlang::quo(1:10), y = quote(x * 2)))

1
  • 1
    You should probably use as.list(rep(NA, length(columnsToAdd))) to save hard coding the number of NAs - especially since the question mentioned a case with 30...
    – Spacedman
    Feb 14, 2019 at 15:44
1

What you are seeing in tibble::add_column(columnsToAdd = NA) is the quasi-something evaluation that dplyr and tidyr introduced. If you check the definition:

> args(add_column)
function (.data, ..., .before = NULL, .after = NULL) 

you'll see that it doesn't expect a certain variable. It literally expects the actual variable name, without quotation.

An entirely different approach is to create a matrix (or data.frame, whatever tickles your fancy), and smack it onto the side of someTibble:

extra <- matrix(NA_real_, nrow=nrow(someTibble), ncol=length(columnsToAdd), dimnames=list(NULL, columnsToAdd))
dplyr::bind_cols(someTibble, as.data.frame(extra))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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