The mutate function is not updated

I have one tbl_df

with multiple columns. I am using a simple mutat to update a column, but the column is not updating.

mutate(fltr, cat = "xxxxx")

      

cat

is a column that is either empty

or NA

. Filter - tbl_df

. What could be causing this? I tried to put the text in the column cat

so that it is not blank or NA in case of a problem. It still didn't work.

+3


source to share


3 answers


As @DavidRobinson pointed out, you are not assigning it back to the same object. To avoid reassignment, we could use the magrittr connection continuum operator % <>% :



require(dplyr)
require(magrittr)

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

      

+5


source


mutate

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



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

      

+6


source


Mutate is to add new columns (which are functions of existing columns) to the existing file frame. Here you want the static column to be added to the dataframe for which I don't feel like you need to mutate. What you can achieve simply like this:

fltr $ cat <- "xxxxx"

But if you want to add a new column based on an existing column, you can do:

fltr <- mutate (fltr, cat = "write the logic for converting column A to column B, for example: Col_A / 100")

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

0


source







All Articles