I'm doing a study assignment. I have a small dataframe.
Last_name Salary_dollars Work_experience_months
1 Lyons 2285 24
2 Wilkinson 2351 17
3 Chambers 3654 72
4 Thornton 2121 12
5 Holland 3098 44
I can display information separately, that is, about the worker with the highest salary and the worker with the least experience.
Factory_workers[which.max(Factory_workers$Salary_dollars),]
Factory_workers[which.min(Factory_workers$Work_experience_months),]
How can I arrange this request so that these two conditions are in one request? I need to display information about the worker with the highest salary and the least experience.
Update, full text of the assignment:
Information about five workers of the shop is given.
The columns have names: last name, salary, work experience.
Display data about the worker with the
highest salary and the least length of service.
it seems to me that a relative connection of these two indicators is needed here and the output should be one line.
CodePudding user response:
Last_name <- c("Lyons","Wilkinson","Chambers","Thornton","Holland")
Salary_dollars <- c(2285, 2351, 3654, 2121, 3098)
Work_experience_months <- c(24, 17, 72, 12, 44)
dt <- data.table(Last_name, Salary_dollars, Work_experience_months)
dt[, .SD[Salary_dollars == max(Salary_dollars) |
Work_experience_months == min(Work_experience_months)],
]
Last_name Salary_dollars Work_experience_months
1: Chambers 3654 72
2: Thornton 2121 12
CodePudding user response:
Following is a dplyr option.
Assuming the dataframe = df as per response from jadel
bind_rows(dt %>% slice_max(Salary_dollars,n=1,with_ties=FALSE), #slice_max() will filter the first row with highest "Salary_dollars"
dt %>% slice_min(Work_experience_months,n=1,with_ties=FALSE)) # slice_min() is opposite of above based on "Work_experience_months"
Output
Last_name Salary_dollars Work_experience_months
1 Chambers 3654 72
2 Thornton 2121 12