Transposing Data from selected columns only

I have a table as below:

However, the Months go as far as from November 2022 to May 2023, I would like to transpose my Table such that the contents of Column Indicators are used as Column labels and the age bands are rows, but keeping District to Week Ending as it is.

Is there anything I can do? I have tried Pivot Longer and it doesn't desired results

You did not provide any copy-friendly data and the data in your image are much too complicated to reproduce by hand. I invented a simple data set and used to pivot functions to reshape it. Is this the kind of thing you want to do?

#Invent data
DF <- data.frame(District = c("A","A","A","A","B","B","B","B"),
                 Muni = c("C","C","D","D","E","E","F","F"),
                 Indicator = c("G","H","G","H","G","H","G","H"),
                 X15_19 = sample(0:3,size = 8, replace = TRUE),
                 X20_24 = sample(0:3,size = 8, replace = TRUE),
                 X25_29 = sample(0:3,size = 8, replace = TRUE),
                 X30_34 = sample(0:3,size = 8, replace = TRUE))
DF
#>   District Muni Indicator X15_19 X20_24 X25_29 X30_34
#> 1        A    C         G      2      2      1      3
#> 2        A    C         H      2      1      3      3
#> 3        A    D         G      1      2      0      2
#> 4        A    D         H      1      0      3      2
#> 5        B    E         G      3      1      0      0
#> 6        B    E         H      3      3      0      2
#> 7        B    F         G      3      3      3      1
#> 8        B    F         H      0      1      1      0
library(tidyr)
#Pivot twice
DF |> pivot_longer(cols = starts_with("X"),names_to = "Age") |> 
  pivot_wider(names_from = "Indicator",values_from = "value")
#> # A tibble: 16 × 5
#>    District Muni  Age        G     H
#>    <chr>    <chr> <chr>  <int> <int>
#>  1 A        C     X15_19     2     2
#>  2 A        C     X20_24     2     1
#>  3 A        C     X25_29     1     3
#>  4 A        C     X30_34     3     3
#>  5 A        D     X15_19     1     1
#>  6 A        D     X20_24     2     0
#>  7 A        D     X25_29     0     3
#>  8 A        D     X30_34     2     2
#>  9 B        E     X15_19     3     3
#> 10 B        E     X20_24     1     3
#> 11 B        E     X25_29     0     0
#> 12 B        E     X30_34     0     2
#> 13 B        F     X15_19     3     0
#> 14 B        F     X20_24     3     1
#> 15 B        F     X25_29     3     1
#> 16 B        F     X30_34     1     0

Created on 2023-04-26 with reprex v2.0.2

2 Likes

I am getting the error below, I am still trying to sort it out though. My Table has 17 000 entries and 23 columns.

Error in values[spec$.name]:
! Can't subset columns with spec$.name.
:heavy_multiplication_x: Subscript spec$.name can't contain the empty string.
:heavy_multiplication_x: It has an empty string at location 24.
Run rlang::last_error() to see where the error occurred.
Warning message:
Values from value are not uniquely identified; output will contain list-cols.

  • Use values_fn = list to suppress this warning.
  • Use values_fn = {summary_fun} to summarise duplicates.
  • Use the following dplyr code to identify duplicates.
    {data} %>%
    dplyr::group_by(District, Municipality, Facility.Name, Month, Year, Weekending, Age_Gender, Indicator) %>%
    dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
    dplyr::filter(n > 1L)

It is very hard to tell what the problem is without seeing your code and some data. You can post a subset of your data by posting the output of

dput(head(DF, 20))

where DF should be replaced by the name of your data frame. Put a line with three back ticks just before and after the pasted output of dput(), like this
```
dput() output goes here
```
Also post your code that leads to the error.

This is how it looks like

structure(list(District = c("fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District", "fs Thabo Mofutsanyana District",
"fs Thabo Mofutsanyana District"), Municipality = c("fs Maluti A Phofung LM",
"fs Maluti A Phofung LM", "fs Maluti A Phofung LM", "fs Maluti A Phofung LM",
"fs Maluti A Phofung LM", "fs Maluti A Phofung LM", "fs Maluti A Phofung LM",
"fs Maluti A Phofung LM", "fs Maluti A Phofung LM", "fs Maluti A Phofung LM",
"fs Maluti A Phofung LM", "fs Maluti A Phofung LM", "fs Maluti A Phofung LM",
"fs Maluti A Phofung LM", "fs Maluti A Phofung LM", "fs Maluti A Phofung LM",
"fs Maluti A Phofung LM", "fs Maluti A Phofung LM", "fs Maluti A Phofung LM",
"fs Maluti A Phofung LM"), Facility.Name = c("fs Bluegumbosch Clinic",
"fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic",
"fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic",
"fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic",
"fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic",
"fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic",
"fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic", "fs Bluegumbosch Clinic",
"fs Bluegumbosch Clinic"), Month = c("November", "November",
"November", "November", "November", "November", "November", "November",
"November", "November", "November", "November", "November", "November",
"November", "November", "November", "November", "November", "November"
), Year = c(2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L, 2022L,
2022L, 2022L, 2022L, 2022L), Weekending = c("Week 1", "Week 1",
"Week 1", "Week 1", "Week 1", "Week 1", "Week 1", "Week 1", "Week 1",
"Week 1", "Week 1", "Week 1", "Week 1", "Week 1", "Week 1", "Week 1",
"Week 1", "Week 1", "Week 1", "Week 1"), Indicator = c("Number of people offered HIV Self Screening services - Community",
"Number of clients who accept HIV Self Screening services - Community",
"Number of HIVST blood-based kits administered by provider (assisted) - Primary distribution - Community",
"Number of HIVST oral-based kits administered by provider (assisted) - Primary distribution - Community",
"Number of HIVST blood-based kits administered by provider (Unassisted) - Primary distribution - Community",
"Number of HIVST oral-based kits administered by provider (Unassisted) - Primary distribution- Community",
"Number of HIVST kits oral-based kits administered outside a health facility (unassisted) - Secondary distribution - Community",
"Number of HIVST kits blood-based kits administered outside a health facility (unassisted) - Secondary distribution - Community",
"Number of clients first ever testing for HIV -Community", "Number of clients tested within 0-3 months - Community",
"Number of clients tested between 4 and 12 months - Community",
"Number of clients tested more than 12 months (a year) ago - Community",
"Number of people screened HIV negative using blood-based kits - Community",
"Number of people screened HIV negative using oral-based kits - Community",
"Number of people screened HIV positive escorted to consultation room using blood-based kits - Community",
"Number of people screened HIV positive escorted to consultation room using oral-based kits - Community",
"Number of people confirmed HIV positive using the national algorithm - Community",
"Number of people linked into HIV care and treatment services - Community",
"Number of people started on ART on the same day of HIV diagnosis - Community",
"Number of people screened for TB, STIs and NCDs (Hypertension, Diabetes) - Community"
), X15...19.F = c(1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 1), X20...24.F = c(2, 2, 0, 0, 0, 0, 0, 2, 0, 2,
0, 0, 2, 0, 0, 0, 0, 0, 0, 2), X25...29.F = c(3, 3, 0, 0, 0,
0, 0, 3, 0, 3, 0, 0, 3, 0, 0, 0, 0, 0, 0, 3), X30...34.F = c(2,
2, 1, 0, 0, 0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2), X35...39.F = c(1,
1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1), X40...44.F = c(1,
1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1), X45...49.F = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X50..F = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X15...19.M = c(1,
1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1), X20...24.M = c(1,
1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1), X25...29.M = c(1,
1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1), X30...34.M = c(3,
3, 0, 0, 0, 0, 0, 3, 0, 2, 1, 0, 3, 0, 0, 0, 0, 0, 0, 3), X35...39.M = c(1,
1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1), X40...44.M = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X45...49.M = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), X50..M = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
20L), class = "data.frame")

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.