8
votes

It seems as if creating a column with dplyr::mutate() does not allow vector recycling. Why?

Example:

require(dplyr)
df <- data_frame(id = rep(1:5, each = 42), name = rep(letters[1:7], each = 6, times = 5))

now:

df %>% mutate (tp = c(1:42))  #results in 

Error in mutate_impl(.data, dots) : 
  Column `tp` must be length 210 (the number of rows) or one, not 42

but of course

df$tp <- c(1:42) #works

Is my mutate code wrong or does recycling simply not work in mutate()? If it helps, I am using dplyr 0.7.2 with RStudio 1.0.153 (Mac)

1
The error message makes it pretty clear you either have to specify all the values or one of them. No recycling. - Spacedman
Fair enough. Well, thanks. I just thought it might be possible but that my code was wrong. Sad times. No recycling. - tjebo
If you need recyclying then as a workaround you can use transform. Compare BOD %>% transform(a = 1:2) and BOD %>% mutate(a = 1:2) . - G. Grothendieck
Thanks! @G.Grothendieck, transform is a great idea :) - tjebo

1 Answers

7
votes

You asked two different questions:

  • (title) "Does dplyr::mutate() not recycle vectors?"

A: no (as you found out). The dplyr vignette makes it clear that recycling only works for length-1 vectors:

They [vector values used in mutate()] must be either length 1 (they then get recycled) or have the same length as the number of rows.

  • Why?

A: Probably impossible to answer without asking the dplyr developers. I would speculate that they reached a different conclusion about the tradeoff between convenience (on the one hand) and requiring explicit statements of user intentions (on the other hand) from the original developers of R (who even allowed incomplete recycling, albeit with a warning).