"Find Replace"-function for dataframes

Hi there!

I am using an algorithm, which produces a dataframe, that can be saved as a csv file. I would like to change every instance of let's say the digit 2 to 5 and 3,6 and 9 to 4, meaning 22735 should become 55745, anywhere in the dataframe. If I try to use the find-replace function for the csv-file (after exporting) in an editor, unfortunately the order of the entries is changed as well. [For the csv-file to work, the first coloumn needs to count from 1 forward by 1, stating the row number within the dataframe. If I change all the values now, the file is unusable. Since I have a lot of rows, I need to replace all entries at once, and cannot check every single one ]
Therefore I do believe I need to change the values within R before exporting the dataframe.

So is there a "Find-Replace" function which looks at the entire dataframe?

You probably want to convert the numbers to string, then replace, then convert back to numbers. Something like:

df %>% mutate_all(str_replace_all, "2", "5")
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.