I am trying to work on the names_pattern agument in pivot_longer and I am not sure I understand the grouping function and how it works. I need to pivot the following data frame to match the desired output below.
df<-structure(list(Weighted_Ideology =0.514, Weighted_Ideology_se = 0.00, Unweighted_Ideology = 0.51, Unweighted_Ideology_se = 0.004), row.names = c(NA, -1L), class = "data.frame")
library(tidyr)
df%>%
pivot_longer(., cols=everything(), names_to=c('Variable', ".value"), names_pattern="([a-z]+_[a-z]+)_(.*)")
Desired Output
df2<-data.frame(
Variable=c('Weighted', "Unweighted"),
Ideology=c(0.54, 0.51),
se=c(0.005, 0.004)
)