R - Data Analysis

From Training Material
Revision as of 09:02, 5 March 2016 by Bernard Szlachta (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


Exercises

45. Exercise

Find employees earning more than the manager’s average.

      ENAME  SAL
1      BUSH 5000
2     BLAIR 2850
4     PUTIN 2975
10    TOOSK 3000
12 CARNEGIE 3000

emp[SAL > mean(emp[JOB=='MANAGER',]$SAL),c("ENAME","SAL")]

46. Exercise*

Select employees earning the maximum salaries in their positions (jobs). Sort results by salary. Hint: Use data.table package.

      ENAME       JOB  SAL
1  CARNEGIE   ANALYST 3000
2     TOOSK   ANALYST 3000
4    ELISON     CLERK 1300
9     PUTIN   MANAGER 2975
10     BUSH PRESIDENT 5000
12  BAROSSO  SALESMAN 1600

emp.maxsal <- merge(emp,emp.dt[,max(SAL),by=JOB])

with(emp.maxsal,emp.maxsal[SAL==V1,c("ENAME","JOB","SAL")])

47. Exercise*

Find employees hired first in their departments.

      ENAME DEPTNO   HIREDATE
3    MERKEL     10 1981-06-09
5  THATCHER     20 1980-12-17
14  BAROSSO     30 1981-02-20

emp.minhiredate <- merge(emp,emp.dt[,min(HIREDATE),by=DEPTNO])

with(emp.minhiredate,emp.minhiredate[HIREDATE==V1,c("ENAME","DEPTNO","HIREDATE")])

48. *Exercise

Generate a report, which computes the following sums.

            10   20   30
ANALYST     NA 6000   NA
CLERK     1300 1900  950
MANAGER   2450 2975 2850
PRESIDENT 5000   NA   NA
SALESMAN    NA   NA 5600