How can I group a set of values by column using R programming?

0 votes

This is my Table:

Employee Details:

EmpID | WorkingPlaces | Salary
1001  | Bangalore     | 5000
1001  | Chennai       | 6000
1002  | Bombay        | 1000
1002  | Chennai       | 500
1003  | Pune          | 2000
1003  | Mangalore     | 1000

Suppose that, the same employee works for different places in a month. 

Using this info how can I find the top 2 highly paid employees?

The output should look like this:

EmpID | WorkingPlaces | Salary
1001  | Chennai       | 6000
1001  | Bangalore     | 5000
1003  | Pune          | 2000
1003  | Mangalore     | 1000
Apr 25, 2019 in Data Analytics by Tyrion anex
• 8,380 points
76 views

2 answers to this question.

0 votes

Use the dplyr package that R provides:

library(dplyr)
df1 %>%
   group_by(EmpID) %>% 
   mutate(SumSalary = sum(Salary)) %>% 
   arrange(-SumSalary, EmpID) %>% 
   head(4) %>%
   select(-SumSalary)
answered Apr 25, 2019 by Sophie may
• 9,940 points
0 votes

Try this ,

Employee %>% group_by(EmpID) %>% mutate(SumSalary = sum(Salary)) %>% arrange(desc(SumSalary)) %>% head(2) %>% select(-SumSalary)
answered Aug 14, 2019 by anonymous

Related Questions In Data Analytics

+1 vote
1 answer

How do I sum the values of a variable by group but RETAIN all records?

Hey @Kirk, what you can do is, ...READ MORE

answered Jun 6, 2019 in Data Analytics by Kalgi
• 51,890 points
137 views
0 votes
1 answer

Shiny r ,I'm doing a dashboard and I can not replace in the table below the name of the column by choosing the selectinput.

Hi, When you want to change any input ...READ MORE

answered Aug 19, 2019 in Data Analytics by anonymous
• 32,460 points
91 views
0 votes
1 answer

How can I delete multiple values from a vector in R?

The %in% operator tells  which elements are ...READ MORE

answered Apr 26, 2018 in Data Analytics by shams
• 3,600 points
2,281 views
0 votes
2 answers
0 votes
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 32,460 points
327 views
+9 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 8, 2018 in Data Analytics by Abhi
• 3,680 points
165 views
0 votes
1 answer

How can I perform word stemming in R

The tm package in R provides the stemDocument() function to stem the ...READ MORE

answered Aug 20, 2018 in Data Analytics by Abhi
• 3,680 points
217 views
0 votes
1 answer

How to know the type of an object?

To find the type of an object ...READ MORE

answered Sep 19, 2018 in Data Analytics by Abhi
• 3,680 points
59 views
0 votes
1 answer

How to create a 2D array of vectors of different lengths in R programming?

You can try making a list of matrices ...READ MORE

answered Feb 1, 2019 in Data Analytics by Sophie may
• 9,940 points
83 views
0 votes
1 answer

Can we have an if loop inside a for loop in R programming?

You're If loop doesn't have any condition ...READ MORE

answered Dec 21, 2018 in Data Analytics by Sophie may
• 9,940 points
71 views