I would like to conditionally mutate numeric values across several columns; I have managed to do it and it works, but it looks super clumsy. Is it possible to write this in a nicer way?
Here an extract of the dataset:
I would like to conditionally change the value of all the numerical variables:
If the value is less then 10, I would like it replaced with NA (or ideally leave it blank if possible?)
If the value is more than 33, I would like it replaced with 33,
if the value is between 10and 33 then it should remain the same.
has greater legibility. Rather than repeating this code block for all six numeric variables, however, make it into a function and use mutate_if [numeric].
I have tried to reprex it and I can't get it to work. Also I can't really post more data than I already have. Not sure why you would need anything else.
In order to give you a working solution, we need sample data in a copy/paste friendly format (we can't copy from a screenshot)
A proper reproducible example makes it much easier to understand your issue and figuring out how to help, also, it is a polite thing to do when asking questions on programming forums in general.
Using the reprex package makes your reproducible example look nicer, but it is not mandatory for providing one, it would be enough to provide sample data in a copy/paste friendly format (with dput() or datapasta::df_paste()) and the minimal runnable code that reproduces your issue.
The solution offered by PossumHound is great, but for completeness, the mutate_if() and in general the scoped versions of mutate() and summarise() have been superseded by the across() function. So, in this new regime, PossumHound's solution would be: