I'm trying to get the right regex for names_pattern()
of pivot_longer()
.
- Wide Data:
ID WC_L1 WC_L2 Read_F_L1 Read_F_L2 Read_C_L2 APL_L1 APL_L2
<glue> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Person1 55 84 70 73 80 34 40
2 Person2 88 102 66 140 80 51 45
3 Person3 102 71 59 59 60 67 46
4 Person4 53 43 61 70 60 33 30
5 Person5 87 145 73 107 80 56 72
- Desired output:
Get all names between _ and pivot them all into their columns, create a "group" column based on what is after the last _ (L1/L2)
ID WC READ_F READ_C APL GROUP
. . . . L1
. . . . L2
This is very similar to this, but I couldn't get the regex right. Thanks in advance!
Data:
structure(list(ID = structure(c("Person1", "Person2", "Person3",
"Person4", "Person5"), class = c("glue", "character")), WC_L1 = c(55,
88, 102, 53, 87), WC_L2 = c(84, 102, 71, 43, 145), Read_F_L1 = c(70,
66, 59, 61, 73), Read_F_L2 = c(73, 140, 59, 70, 107), Read_C_L2 = c(80,
80, 60, 60, 80), APL_L1 = c(34, 51, 67, 33, 56), APL_L2 = c(40,
45, 46, 30, 72)), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))