20621

filter and unfilter in dplyr

Question:

What would be the dplyr analog of performing the following operation in base R?

iris$Sepal.Length[iris$Sepal.Length>2] <- iris$Sepal.Length[iris$Sepal.Length>2] * 10

I am trying to use filter but cannot go back to the original data set (without a join)

Answer1:

You can use mutate with ifelse to get the same results as comments from @alistaire:

iris %>% mutate(Sepal.Length = ifelse(Sepal.Length > 2, Sepal.Length * 10, Sepal.Length))

Answer2:

If we are using data.table, we can avoid the ifelse and make it faster

library(data.table) as.data.table(iris)[Sepal.Length > 2, Sepal.Length := Sepal.Length * 10]

Recommend

  • understanding difference in results between dplyr group_by vs tapply
  • Delete in PhotoKit: can you skip “Recently Deleted”?
  • how to execute (exec) external system commands in Scala actor?
  • Sending and receiving data via POST in Microsoft Word 2011 for OSX
  • Preprocessing csv files to use with tflearn
  • Add a geom_rect to the plot background (not panel) in ggplot2
  • R do something after a warning (like tryCatch a warning, then edit an object)
  • How can I maintain a color scheme across ggplots, while dropping unused levels in each plot?
  • Export weka results on command line
  • Count row with condition per group
  • Is there an apache pig equivalent of “SHOW TABLES”?
  • ANOVA on multiple responses, by multiple groups NOT part of formula
  • How to get rows with min values in one column, grouped by other column, while keeping other columns?
  • Use conditional coloring on a plotly surface
  • How to provide hyperlink in email pointing to a specific method inside gwt app (but not main page)
  • what does the follow prolog codes do?
  • Hadoop shuffle uses which protocol?
  • FTS3 searches in ORMLite?
  • In matplotlib, how do you change the fontsize of a single figure?
  • Spring: No transaction manager has been configured
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Authentication in Play! and RestEasy
  • Object and struct member access and address offset calculation
  • dc-js disable selecting slices on click for pie chart
  • Spring security and special characters
  • Use group_by to filter specific cases while keeping NAs
  • retrieve vertices with no linked edge in arangodb
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal