Dear community,
I want to switch a dataframe from wide to long format.
The variables look like this (more than 500 vars), e.g.:
pid, d1reputin, d1dispu, d2reputin, d2dispu, d3reputin, d3dispu, w1wiss, m1gradv, w2wiss, m2gradv, w3wiss, w3gradv usw.
So the variables are separated/identified by
- a letter in the first place, followed by
- a number (representing the survey period), finishing with
- an individual varname.
Having just started working with R, I did not come too far with this task.
I have tried options with pivot_longer using names_pattern and names_sep...
The examples for both options usually deal with an underscore such as
names_sep = "_"
but donĀ“t give solutions for tidying data with two separators or irregluar ones such as letters and numbers.
I managed to start in a simple way for a single set of similar vars/small subset but did not really come further than this:
longdata <- widedata %>%
pivot_longer(names_to = "survey",
values_to= "wiss",
cols = 2:5,
)
This gives me dataframe in long format for the variables w1wiss to w5wiss with the vars "pid" "survey" (w1, w2, w3, w4, w5) and "wiss" (1,2,3,4,5) but still is far from what I am looking for...
Are there any experienced users of pivot_longer around here who have some hints for me how to solve this?
Thank you!