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
11 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,050 points

Related Questions In Data Analytics

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
51 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
18 views
0 votes
1 answer

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$symb ...READ MORE

answered Jan 11 in Data Analytics by Tyrion anex
• 8,280 points
47 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
52 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
35 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
20 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
• 35,800 points
14 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,050 points
34 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.