I have the following dataframe
V1 V2
A20
Bxy
C3a
D val1
D val2
D val3
A30
Bij
C4b
D val4
D val5
I wish to create level style dataframe as the output:
V1 V2 levels
D val1 A20-Bxy-C3a
D val2 A20-Bxy-C3a
D val3 A20-Bxy-C3a
D val4 A30-Bij-C4b
D val5 A30-Bij-C4b
I tried to use mutate and grepl, step-by-step as in:
df_level = df %>% mutate(levelA = grepl('^A',df$V1),
levelB = grepl('^B',df$V1),
levelC = grepl('^C',df$V1))
What I get is three columns with levelA, levelB, levelC with logicals.
How to copy the row value, if it matches the grep and finally join to make a consolidated levels?