Where to insert data cleaning code in job script?

Hi there!

I have a job of which a section of an alterState call contains the script captured below. I was looking to reassign some of the values from a data variable depending on the values of 2 other variables in the dataset, but I don’t know where I should insert the line of code to do this in the job script.

The idea was to assign the value of the variable bns_matrix_radio/bns_matrix_radio_number to 1 if:

  • the value of bns_matrix_radio/bns_matrix_radio_possess is ‘yes’ AND
  • the value of formName is ‘BNS Makira 2019’.

If these conditions are not met, the value of bns_matrix_radio/bns_matrix_radio_number should stay as it is.

The code I had in mind to do this would look a bit like this:
‘NAME OF COLUMN IN DATABASE TABLE THAT HOLDS VALUES OF bns_matrix_radio_number’: state => (dataValue(‘body.bns_matrix_radio/bns_matrix_radio_possess’) === ‘yes’ && dataValue(‘formName’) === ‘BNS Makira 2019’ ? 1 : dataValue(‘body.bns_matrix_radio/bns_matrix_radio_number’)),

Where would you recommend to insert this data cleaning code in the job script? Would it be in-between the curly bracket and the bracket of line 92 (before closing the .map called line 69), or on line 93 so it is separate from what’s happening in lines 67 to 92, or somewhere else?

Thanks for your help!

Charlotte

Hey @charlotte welcome to our forum.

If I understood your request correctly you would want to assign a variable based on different values. The question I would ask you is: is the variable you want to assign a ‘key’ inside the variable matrix?
If no, you could do something as your code say plus some minor changes and put it in line 93.

bns_matrix_radio_number = 
  dataValue("body.bns_matrix_radio/bns_matrix_radio_possess")(state) === "yes" &&
  dataValue("formName")(state) === "BNS Makira 2019"
    ? 1
    : dataValue("body.bns_matrix_radio/bns_matrix_radio_number")(state);

Now if you answered yes to my question above that means you can put it inside the map and have

[...]
return { // line 74
    ...
  NEW_COLUMN: state => dataValue("body.bns_matrix_radio/bns_matrix_radio_possess")(state) === "yes" &&
  dataValue("formName")(state) === "BNS Makira 2019"
    ? 1
    : dataValue("body.bns_matrix_radio/bns_matrix_radio_number")(state);
}

Hi @Mamadou,

Thanks for your quick reply!
I think the answer to your question is yes. The variable bns_matrix_radio/bns_matrix_radio_number already exists in the dataset, we would not be creating a new variable.

Ok so if we were to insert this data cleaning code inside the map, does it matter where exactly we insert it inside the map? For example would it work the same way if it were inserted after line 74, 78, or 90?

Charlotte

Hi @charlotte
No, it does not matter at all, as long as it is between the opening and closing brackets of the return.