4

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
mo_maat
  • 2,110
  • 12
  • 44
  • 72

3 Answers3

8

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")
David Robinson
  • 77,383
  • 16
  • 167
  • 187
  • Forgive me if I am posting to the wrong area. But essentially here is what I am trying to do: I have a table and I would like to update certain columns based on the value in another column. So I want to update column B based on the value in Column A matching any one row from another table. To do the match I am using grepl. I'm open to any other ways to handle such a task. I've been toying with dplyr, data.table and even just using for loops with a data frame. – mo_maat Apr 16 '15 at 20:04
  • Here is what I have so far: data %>% filter(grepl("SearchString",ColumnA)) %>% mutate(ColumnB = "xxxx")%>% I plan to make this an iterative process for all values of "SearchString" which comes from another table/list/vector and for all items in the tbl_df data. – mo_maat Apr 16 '15 at 20:07
  • @user1991118 It sounds like you want dplyr's "join" functions, probably `inner_join`, if you're matching a column from one table to another. You should probably ask this as a new question, though. (Make sure you make it a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)) – David Robinson Apr 16 '15 at 20:09
4

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")
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Thanks for the suggestion. I think too that you might have meant %>%?? – mo_maat Apr 16 '15 at 19:29
  • 2
    It is not a typo, it is `%<>%`. – zx8754 Apr 16 '15 at 19:31
  • I see. So why does this not seem to work? data %>% filter(grepl("bis",ItemName)) %>% mutate(Cat = "xxxx")%>% print – mo_maat Apr 16 '15 at 19:58
  • Maybe: `data %<>% mutate(Cat = ifelse(grepl("bis",ItemName),"xxxx",Cat))`. Please add reproducible data to your post, and expected output. – zx8754 Apr 16 '15 at 20:10
  • That worked! So now my challenge is being able to do exactly the same thing but by looping through a list/vector or whatever for the search strings to apply to Cat. Something like this where cat1 is a list of some sort. `for (x in 1:length(cat1)){ data %<>% mutate(Cat = ifelse(grepl(cat1[i],ItemName),"xxx",Cat)) }` I am open to any better way of accomplishing this. – mo_maat Apr 16 '15 at 20:20
  • No need for loops, use `|` - OR operator within `grepl()`, for example: `data %<>% mutate(Cat = ifelse(grepl("bis|bip|bin",ItemName),"xxx",Cat))` – zx8754 Apr 16 '15 at 20:46
-1

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.

Shalini Baranwal
  • 2,780
  • 4
  • 24
  • 34