dfe-analytical-services / dfeR

Common R tasks in the Department for Education (DfE)
https://dfe-analytical-services.github.io/dfeR/
GNU General Public License v3.0
8 stars 2 forks source link

Add a 'z replace' function #66

Open cjrace opened 5 months ago

cjrace commented 5 months ago

Is your feature request related to a problem? Please describe.

Have seen this used in quite a few projects. We could neaten that by providing it centrally in a way linked to the data screener.

Describe the solution you'd like

A function that pulls from the EES data screener repo to get the potential time and location cols, then replaces NAs with 'z', the GSS symbol for not available.

# Replace NAs with zs in the right places------------------------

z_replace <- function(table_name) {
  table_name %>% # take name of table you want
    mutate_at(vars(-region_code, -region_name, -old_la_code, -new_la_code, -la_name), ~ replace_na(., "z")) # replace all NAs apart from geographic data with "z"
}

Describe alternatives you've considered

Could expand this a little to take in more inputs, and also replace specific values. 'x' would be suitable for not available for example.

Would be worth doing a quick speed test against a data.table equivalent, as we're putting into a function we can care less about readability behind the scenes and more about speed (especially as this could be applied to some large datasets!).

Additional context

None.