I am trying to group 2 columns given a third in RStudio. The actual table I have is this. What I would like to have is this. As you can see, the county names Attock
, Bahawal
, etc... repeat themselves 21 times for each of the services (tap
, pump
, etc...).
What I would like to have is the county names repeated only once in the top row, one column with the 21 services and the 2nd column with the respective population.
Anyone know how to do this, please? I've been trying different codes and functions but it doesn't seem to work.
df <- data.frame(District_name = c(rep('Attock', 4), rep('Bahawal', 4)),
Services = rep(c('Tap', 'Pump', 'Well', 'Spring'), 2),
RHH_Access = c(46, 53, 62, 58, 98, 102, 58, 63))
District_name | Services | RHH_Access |
---|---|---|
Attock | Tap | 46 |
Attock | Pump | 53 |
Attock | Well | 62 |
Attock | Srping | 58 |
Bahawal | Tap | 98 |
Bahawal | Pump | 102 |
Bahawal | Well | 58 |
Bahawal | Srping | 63 |
I have 36 districts and 21 services. I made some progress using the code from @moodymudskipper but still not there: Here is what I get now:
Services | Attock | Bahawal | Bhakkar |
---|---|---|---|
Tap | c(46, 78) | c(98, 152) | c(90, 132) |
Pump | 53 | 102 | 53 |
Well | c(62, 68) | c(58, 36) | c(65, 10) |
Spring | 58 | 63 | 25 |
This happens for tap and well because I have these services twice in the data.