Group dataframe based on a column

Hi,

I have a dataframe called table4 with many columns including the diff_charge column. i need to group the dataframe .
The only column that can added to the parenthesis of group_by is the diff_charge column which is found by (diff_charge = amount_courier - total_charges). But here's the thing it is possible for 2 diffrent order_id to have same diff_charge. In this case how will i should i group the dataframe.

if you look at this image the first 3 rows are identical, so instead of 3 duplicate rows how can I replace it with a single row.

link to table4 excel file

a. You can group-by multiple multiple variables in the 'group-by' argument, something like: table4 %>% group_by(order_id, diff_charge).
b. For removing rows that are complete duplicates of each other, you could simply use the 'distinct()' function from dplyr.

1 Like

Hey thanks for the reply.
Is there a way to view the final dataframe after group_by is used. When I tried using the group_by, R reads it but never displays the grouped data

Yes, you only need to assign the resultant data frame to a variable.
ex. newdf <- table4 %>% group_by(order_id, diff_charge)
View(newdf)
It will be useful to check the documentation for 'dplyr' on CRAN for all kinds of data manipulation.

tried this but it doesn't work.

What I want the data to look like:
First row has the values corresponding to diff_charge = 50.1. the second row has values corresponding to diff_charge =84.9. and so, on

Grouping does not change the order of the rows. Use dplyr's arrange() function to do that.

Are you expecting grouping to change the total number of rows in your set ?
if so you want grouped aggregation, or grouped summarisation ... in R/tidyverse simply grouping things changes the behaviour of verbs you might apply to the frame but other than that its simply metadata and does not summarise in itself.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.