Back

Explore Courses Blog Tutorials Interview Questions
+3 votes
7 views
in R Programming by (3.9k points)
edited by

I have a dataframe:

> myvec
    name order_no

1   Nehal       12
2   sejal       14
3   sejal       16
4   shyam       11
5   Nehal       12
6   Sejal       16
7   Ram       19

For each name how to count the distinct order no. Value. I am expecting this output:

name    number_of_distinct_orders
Neha     
2
Sejal       3
Shyam   1
Ram       1

1 Answer

0 votes
by (46k points)
edited by

If you have plyr package then use this:

ddply(myvec,~name,summarise,number_of_distinct_orders=length(unique(order_no)))

else use data.table approach

library(data.table)
DT <- data.table(myvec)

DT[, .(number_of_distinct_orders = length(unique(order_no))), by = name]

Related questions

+3 votes
1 answer
asked May 29, 2019 in R Programming by Anvi (10.2k points)
+4 votes
2 answers
asked May 29, 2019 in R Programming by Krishna (2.6k points)
+1 vote
1 answer
asked May 23, 2019 in R Programming by Nigam (4k points)
+1 vote
1 answer

Browse Categories

...