0

I have data like:

Userkey.             Qty
420151.                5
421052.                8
421053.                3
421052.                6
421053.                4
421052.                7

What I want:

Userkey.             Sumofqty.           Countofqty(frequency)
421051.                    5.                               1
421052.                   21.                               3
421053.                    7.                               2
camille
  • 16,432
  • 18
  • 38
  • 60
  • 1
    I've improved the formatting of the example data, but please also try to include it in a [copy-pasteable format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Marius Aug 23 '19 at 04:35
  • Add some more description on your question(doubt) and what result you are expecting. Explain in detail regarding your doubts and what other ways you have tried. – Pradip Tilala Aug 23 '19 at 04:47

1 Answers1

0

This tidy solution should work:

library(tidyverse)

data <- tribble(~Userkey, ~Qty,
                "420151.", 5,
                "421052.", 8,
                "421053.", 3,
                "421052.", 6,
                "421053.", 4,
                "421052.", 7)


data %>% 
  group_by(Userkey) %>% 
  summarise(Sumofqty = sum(Qty), Countofqty = n())
larsoevlisen
  • 313
  • 1
  • 10
  • If I've a CSV file which contains USERKEY and QTY, then what will the code (R Programming) – Shashikant Gupta Aug 23 '19 at 04:43
  • 1
    This depends on the formatting of the file. You should be able to import it (if using the tidyverse packages as loaded above with `library(tidyverse)` by implementing something like `data <- read_csv(file = "path_to_your_file")` instead of the `data <- tribble(...`. You might also need to set some of the other arguments - again depending on the formatting. – larsoevlisen Aug 23 '19 at 04:48
  • If I want to add this summarisation in another data frame, then what is my code. – Shashikant Gupta Aug 23 '19 at 05:12
  • I'm sorry but you should create a new question for this. It depends on the the dimensions and nature of the other data frame. Please accept this answer if it solved your original question - then create a new question for your further inquiries. – larsoevlisen Aug 23 '19 at 07:26