3
votes

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
2
Maybe just iris %>% mutate_each(funs(Replace15), starts_with("Petal")) ?aosmith
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. stackoverflow.com/q/16275149/1191259Frank

2 Answers

3
votes
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.

3
votes

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))