'mutate function is not updating

I have a tbl_df with several columns. I am running a simple mutate on it to update a column, but the column is not being updated.

mutate(fltr, cat = "xxxxx")

cat is a column that is either empty or NA. Filter is a tbl_df. What could be causing this? I have tried to put text in the cat column so that it is not empty or NA in case that was causing the problem. That still did not work.



Solution 1:[1]

mutate doesn't change the tbl_df in place, it just returns the new, changed tbl_df. You need to save the results:

fltr <- mutate(fltr, cat = "xxxxx")

Solution 2:[2]

Mutate is to add new columns(that are function of existing columns) to the existing dataframe. Here you want a static column to be added to the dataframe, for which i dont feel you need mutate. That you can achieve simply like :

fltr$cat <- "xxxxx"

But in case you want to add new column based on existing column, you can do :

fltr <- mutate(fltr, cat = "write your logic of converting column A to column B eg : Col_A/100")

Here "cat" will be the name of column you created.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Robinson
Solution 2 Shalini Baranwal