I have a dataset with various "chunks" of columns with different prefixes, but the same suffix:
| ID | A034 | B034 | C034 | D034 | A099 | B099 | A123 | B123 | ... |
|---|---|---|---|---|---|---|---|---|---|
| 1 | NA | 1 | NA | NA | NA | 3 | 1 | NA | ... |
| 2 | 2 | NA | NA | NA | 2 | NA | NA | 2 | ... |
| 3 | NA | NA | 2 | NA | NA | 2 | 1 | NA | ... |
The number of columns within each "chunk" also varies. Is there any way (other than manually, which is what I have been painstakingly doing with coalesce(!!! select(., contains("XXX")))) to automatically coalesce by chunk based on the shared suffix? That is, the result should resemble
| ID | 034 | 099 | 123 | ... |
|---|---|---|---|---|
| 1 | 1 | 3 | 1 | ... |
| 2 | 2 | 2 | 2 | ... |
| 3 | 2 | 2 | 1 | ... |
I'm not sure how to begin doing something like this, so any suggestions would be very helpful.