3

I am trying to use dplyr to apply a function to a subset of columns. However, in contrast to the code per below, I am trying to implement this while keeping all columns in the data-frame. Currently, the resulting data-frame only keeps the selected columns. I can use a remove-and-cbind construction to merge these columns back into the original dataframe, but I was wondering if there is a way to do this directly in dplyr? I have tried to move the select function inside the mutate function, but was not able to make this work yet.

require(dplyr)
Replace15=function(x){ifelse(x<1.5,1,x)}
dg <- iris %>%
  dplyr::select(starts_with("Petal")) %>%
  mutate_each(funs(Replace15))  
dg

> dg
Source: local data frame [150 x 2]

   Petal.Length Petal.Width
1           1.0           1
2           1.0           1
3           1.0           1
4           1.5           1
5           1.0           1
6           1.7           1
7           1.0           1
8           1.5           1
9           1.0           1
10          1.5           1
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
user1885116
  • 1,757
  • 4
  • 26
  • 39
  • 3
    Maybe just `iris %>% mutate_each(funs(Replace15), starts_with("Petal")) `? – aosmith Jun 25 '15 at 15:43
  • You can also define your function in terms of the built-in `replace` function. `ifelse` can be slow, if that's a concern of yours. http://stackoverflow.com/q/16275149/1191259 – Frank Jun 25 '15 at 15:53

2 Answers2

3
dg <- iris %>% mutate_each(funs(Replace15), matches("^Petal"))

Alternatively (as posted by @aosmith) you could use starts_with. Have a look at ?select for the other special functions available within select, summarise_each and mutate_each.

Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52
3

mutate_each is now deprecated, replaced by mutate_if and mutate_at. Using these replacements, the answer is

dg <- iris %>% mutate_at(vars(starts_with("Petal")), funs(Replace15))
Ken Williams
  • 22,756
  • 10
  • 85
  • 147
GGAnderson
  • 1,993
  • 1
  • 14
  • 25