-2

[!This is how the input looks like [This is how the output should look like]1 I want to sum up all the duplicates in a column so that there is no duplicate row in the data frame.

2 Answers2

1

I would love to test your data, so please provide it using dput.

Based on this post - Remove duplicates and sum values in R perhaps something like

library(plyr)
filtered_df <- ddply(df,~CashierName,summarise,Quantity=sum(Quantity))

another option, with dplyr

df <- df %>% group_by(CashierName) %>% summarise(Quantity= sum(Quantity))
  • I am unable to figure out how to send you the data frame using `dput`. I am a newbie to R and data analytics field. Can you please help me out? Using `dput` in R console is producing some result which is a data frame – Peddinti Kartik May 14 '20 at 13:36
  • 1
    sure @PeddintiKartik. For future reference, you just have to run ```dput(your_data)``` in your R console, and then copy the output and paste it here. When you paste it, it is better that you paste it as code, not as text. for that you have to paste code between this ` symbol. Also see here - https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Pablo Herreros Cantis May 14 '20 at 13:41
  • I ran `dput(my_data)` in my R console. Actually the data frame has 80000 rows. It is just clearing the screen and displaying only last part of the dataframe. – Peddinti Kartik May 14 '20 at 13:47
  • 1
    In that kind of situation, you may first subset your data so that you only share with the SO community a small piece. For example, if you do ```subset <- df[1:100,]``` you will have the first 100 rows of your data. And then you can share the output of dput(subset) – Pablo Herreros Cantis May 14 '20 at 14:10
  • thank you for the information! – Peddinti Kartik May 14 '20 at 14:12
  • You are welcome. Good luck! – Pablo Herreros Cantis May 14 '20 at 14:14
1

Or in data.table

library( data..table )
x <- data.table( x )
x[ , .(Quantity= sum( Quantity)), by = "CashierName" ]
MatthewR
  • 2,660
  • 5
  • 26
  • 37