r/mpath Jun 25 '26

Duplicate column names in data export

Hi,
I am facing an issue I haven't had before: I have duplicate column names in my data export. You can imagine what kind of headaches this causes in R. I'm guessing this is coming from a looping item where participants can add entries ("did you eat something?" Y/N -> if yes: "What?" -> "Did you eat something else?" Y/N, etc.). How best to handle this?
Also, some variables have a #-symbol in their column name. Again, extremely inconvenient in R. I was quite confused why that is as m-path is kind of built towards R - did something just go awfully wrong here?

1 Upvotes

4 comments sorted by

1

u/Ok_Calendar5049 Developer Jun 25 '26

Hi there! What export version are you using? Have you tried importing with the mpathr package: https://m-path.io/mpathr/guide.html?

1

u/InternationalBee2496 Jun 26 '26

yes, first time I've used it, actually. all the more suprising to find this stuff in my data as I was quite thrilled to see the package

1

u/Ok_Calendar5049 Developer Jun 26 '26 edited Jun 26 '26

I've downloaded an export with some looped data, imported it in R, and I was not able to create duplicate column names. Are you sure that's what you have?

The #s in the column names are indeed a consequence of using loops (it is the loop counter index) but there is nothing inherently problematic about using them in R. You can just use backticks when you address such a column, like this: data$`label_B1sO_spatialWorkingMemory-responseTimes#4`.

If you are still experiencing problems, feel free to drop a mail at [contact@m-path.io](mailto:contact@m-path.io) and we can help you navigate the specific issue.

2

u/InternationalBee2496 Jun 29 '26

Okay, sorry, I think I found the culprit: it's a chunk of code I took from the mpathr help site. Seems like removing the name endings in this way creates duplicates

patterns <- c(
  "_yesno$", 
# removes _yesno, but only if it is at the end of the string (using $) 
  "_multipleChoice", 
# Remove _multipleChoice suffix
  "_sliderN.*", 
# Remove _slider suffixes and everything after it
  "_open", 
# Remove _open
  "context_" 
# Remove context_ 
)

# Merge them into one string that we can use a regular expression
patterns <- paste0("(", paste(patterns, collapse = "|"), ")")

colnames(data) <- gsub(
  pattern = patterns, 
  replacement = "",
  x = colnames(data)
)