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 in Data Analytics by Tyrion anex
• 8,280 points
30 views

1 answer 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 by Sophie may
• 9,430 points

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 in Data Analytics by Kalgi
• 37,430 points
30 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,580 points
71 views
0 votes
1 answer
0 votes
1 answer

How can you find total number of null values in a dataset column wise?

You can write a custom sapply function ...READ MORE

answered Oct 12, 2018 in Data Analytics by Anmol
• 3,620 points
25 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
74 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 Anmol
• 3,620 points
41 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 Anmol
• 3,620 points
24 views
0 votes
1 answer

How do I import document files in R programming

Try the following: read.table("~/SMSSpamCollection", header=TRUE, sep="\t") Change "~/" to the "ptah/to/your/file/" Mess around ...READ MORE

answered Nov 5, 2018 in Data Analytics by Kalgi
• 37,430 points
18 views
0 votes
1 answer
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,430 points
39 views