-4

I have a small data frame in R called df1.

Below is how the data frame looks like:

SubDept2        BasicSalary     BenchmarkSalary
Admin             10000          20000
Bar                9880          12000
Entertainment     11960          17000
F&B                9680          12000
Finance           10310          17500
Housekeeping       9960          12000
Kitchen            9680          12000
Leisure & Sport   10775          17000
Maintenance       10240          10000
Restaurant         9880          12000
Rooms Division     9680          12000
Security          10250          11000
Spa                9450          12000

I want to pivot this data frame so that it turns out like this:

SubDept2              Amount     Type
 Admin                10000      BasicSalary
 Bar                   9880      BasicSalary
 Entertainment        11960      BasicSalary
 F&B                   9680      BasicSalary
 Finance              10310      BasicSalary
 Housekeeping          9960      BasicSalary
 Kitchen               9680      BasicSalary
 Leisure & Sport      10775      BasicSalary
 Maintenance          10240      BasicSalary
 Restaurant            9880      BasicSalary
 Rooms Division        9680      BasicSalary
 Security             10250      BasicSalary
 Spa                   9450      BasicSalary
 Admin                20000      BenchmarkSalary
 Bar                  12000      BenchmarkSalary
 Entertainment        17000      BenchmarkSalary
 F&B                  12000      BenchmarkSalary
 Finance              17500      BenchmarkSalary
 Housekeeping         12000      BenchmarkSalary
 Kitchen              12000      BenchmarkSalary
 Leisure & Sport      17000      BenchmarkSalary
 Maintenance          10000      BenchmarkSalary
 Restaurant           12000      BenchmarkSalary
 Rooms Division       12000      BenchmarkSalary
 Security             11000      BenchmarkSalary
 Spa                  12000      BenchmarkSalary

How can this be done?

user3115933
  • 4,303
  • 15
  • 54
  • 94

1 Answers1

-3
library(tidyr)
df %>% gather(value='amount',key='Type',-SubDept2) %>% head(n=5)

       SubDept2        Type amount
1         Admin BasicSalary  10000
2           Bar BasicSalary   9880
3 Entertainment BasicSalary  11960
4           F&B BasicSalary   9680
5       Finance BasicSalary  10310
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • 2
    Please, don't post answers to questions that were asked hundreds of times, mark it as a duplicate. This way you will help OP to find more answers and rich discussion. Also, piping has nothing to do with this question. – pogibas Aug 26 '18 at 10:43
  • Please don't repost deleted comments. If your comment is deleted, it means it didn't belong here in the first place, reposting it will only lead to your reposted comment being deleted again. – user229044 Aug 26 '18 at 22:02