Transpose in dataframe with loops

Hi everyone,

I want to transpose only parts of a dataframe (population data) and this should be done repeatedly for the first rows 1:19, then 20:39, 40:59, and so on.
So my data frame looks roughly like this:
Community Age-cohort Year1 Year2 Year3 ....
1 0-3 numbers numbers
1 3-5
..
2 0-3
2 3-5
...
So the age cohorts and the years should be transposed, the positions of the community should not change. But every Community should be transposed alone and not the whole dataframe. I tried it with loops, but I do not know exactly how to do that. It is for some of you potentially obvious how to do that, but I am an absolute beginner in such tasks.

Thanks a lot for your help.

I think you are looking for something like tidyr::pivot_longer()

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

This topic was automatically closed 21 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.