Search code examples
redcap

How can I join multiple variables into one, without losing information in REDCap?


I am designing a database for a study in REDCap, I come across a problem that I don't really know how to solve since I haven't been using the program for a long time.

In my database, I collect information from several different centers and in each center there are several researchers. The organizer of the project has told me that it is very important that in the final result (I understand the data.frame, since we will do the analysis in R) we obtain a variable that is all the names of the researchers associated with all the observations they have made. each. I was doing it by creating an individual variable for each center and then a variable that selects the work center, so that not too many names appear first. But according to your work site, only the names of said site would appear.

Is there a way to dump that information into a new variable? something like Total researchers names and that is a variable that contains in order each researcher with their observations.

I understand that it can be done easily once we have the data.frame, but the coordinator insists that it is important to do it within REDCap.

I have tried to play with the smart variables and with the conditionals but neither option works for me. I currently have something like this: variable work center -center1 -center2 -center3 .... where each center is a variable that depends on the first work center variable that contains the names of each researcher from that center


Solution

  • You may want to look at the @CALCTEXT() action tag and the concat() function, to concatenate multiple values and store them in a field as a single value. You could even use both of those to automatically append the current user's username to a variable if it doesn't already exist in it. Something like (let's say your var is [researchers]):

    @CALCTEXT(if(not_contain([researchers],[user-name]),concat([researchers],', ',[user-name]),[researchers])) 
    

    That is, if [user-name] is not found in [researchers], append it after a comma. Then in R you could build this into an array by splitting on comma.

    I just tested this on a late version (13.something) and found it generates the right output; a comma separated list of all the usernames who have ever saved the current page, with no duplicates.

    There could be unexpected consequences, so you may want to test more thoroughly. Perhaps it would miss any data saved by import or by the API, as it would be executed on a data entry form (it would 'work' on a survey but the user is always [survey-participant] so that won't help). It might, conversely, inadvertently pick up the username of a user who runs DQ rule H, possibly.

    Edit: Another issue I just thought of is that it might be possible for one username to be a substring of another. Perhaps a user named samantha does some data entry, her username is appended to [reseachers]. Later, user sam does something.

    not_contain('samantha','sam') returns false, so Sam's username is not added to the list of researchers.

    If REDCap had more programmer-friendly functions, like split(text, separator) -> arr and index_of(array, element) -> int, or even just a regex_match(text, re) -> bool, then you could potentially do something like:

    if(index_of(split([researchers],', '),[user-name])=-1),…
    

    or more simply:

    if(regex_match([researcher],'/b\[user-name\]/b'),…
    

    But alas...