mo_maat
mo_maat

Reputation: 2240

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.

Upvotes: 4

Views: 14387

Answers (3)

Shalini Baranwal
Shalini Baranwal

Reputation: 2998

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.

Upvotes: -1

zx8754
zx8754

Reputation: 56159

As @DavidRobinson pointed out, you are not assigning it back to the same object. To avoid reassigning we could use magrittr's compound assignment pipe-operator - %<>% :

require(dplyr)
require(magrittr)

fltr %<>% mutate(cat="xxxxx")

Upvotes: 4

David Robinson
David Robinson

Reputation: 78610

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")

Upvotes: 8

Related Questions