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
iris %>% mutate_each(funs(Replace15), starts_with("Petal"))
? – aosmithreplace
function.ifelse
can be slow, if that's a concern of yours. stackoverflow.com/q/16275149/1191259 – Frank