Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one stop using rowwise in dplyr?

Tags:

r

dplyr

So, if one wishes to apply an operation row by row in dplyr, one can use the rowwise function, for example: Applying a function to every row of a table using dplyr?

Is there a unrowwise function which you can use to stop doing operations row by row? Currently, it seems adding a group_by after the rowwise removes row operations, e.g.

data.frame(a=1:4) %>% rowwise() %>% group_by(a) # ... # Warning message: # Grouping rowwise data frame strips rowwise nature  

Does this mean one should use group_by(1) if you wish to explicitly remove rowwise?

like image 303
Alex Avatar asked Apr 21 '15 03:04

Alex


People also ask

What does rowwise () do in R?

rowwise() allows you to compute on a data frame a row-at-a-time. This is most useful when a vectorised function doesn't exist. Most dplyr verbs preserve row-wise grouping.

How do I delete a row wise in R?

rowwise() is just a special form of grouping, so if you want to remove it from a data frame, just call ungroup() .

How do I sum a row in R Dplyr?

Syntax: mutate(new-col-name = rowSums(.)) The rowSums() method is used to calculate the sum of each row and then append the value at the end of each row under the new column name specified. The argument . is used to apply the function over all the cells of the data frame.

What does row wise mean?

rowwise (not comparable) By rows; one row at a time.


1 Answers

As found in the comments and the other answer, the correct way of doing this is to use ungroup().

The operation rowwise(df) sets one of the classes of df to be rowwise_df. We can see the methods on this class by examining the code here, which gives the following ungroup method:

#' @export ungroup.rowwise_df <- function(x) {   class(x) <- c( "tbl_df", "data.frame")   x } 

So we see that ungroup is not strictly removing a grouped structure, instead it just removes the rowwise_df class added from the rowwise function.

like image 138
Alex Avatar answered Sep 17 '22 19:09

Alex