35

If my data frame (df) looks like this:

Name        State
John Smith  MI
John Smith  WI
Jeff Smith  WI

I want to rename the John Smith from WI "John Smith1". What is the cleanest R equivalent of the SQL statement?

update df 
set Name = "John Smith1"
where Name = "John Smith"
and State = "WI"
4
  • 6
    Maybe like this? df[df$Name == "John_Smith" & df$State == "WI",1] <- "John_Smith1"
    – DatamineR
    Feb 21, 2015 at 20:37
  • RStudent is right on, in case your first column isn't of class factor Feb 21, 2015 at 20:54
  • That almost works, but I have a very big data frame so I was trying to simplify the question. Your solution adds "John_Smith1" to the first column of my data frame, not the df$Name column.
    – Frank B.
    Feb 21, 2015 at 20:55
  • 1
    @FrankB. You can change the 1 to Name as I did in the answer
    – DatamineR
    Feb 21, 2015 at 20:59

4 Answers 4

43
df <- data.frame(Name=c('John Smith', 'John Smith', 'Jeff Smith'),
                 State=c('MI','WI','WI'), stringsAsFactors=F)

df <- within(df, Name[Name == 'John Smith' & State == 'WI'] <- 'John Smith1')

> df
         Name State
1  John Smith    MI
2 John Smith1    WI
3  Jeff Smith    WI

** Edit **

Edited to add that you can put whatever you like in the within expression:

df <- within(df, {
    f <- Name == 'John Smith' & State == 'WI'
    Name[f] <- 'John Smith1'
    State[f] <- 'CA'
}) 
2
  • I wish I knew how to do that economically for updating multiple columns. This syntax appears to require duplicating the subsetting condition for each column being updated. Aug 9, 2019 at 21:53
  • 1
    within takes an expression as the second argument. You can shove whatever you want in there. See my updated answer for an example.
    – Zelazny7
    Aug 10, 2019 at 2:52
23

One way:

df[df$Name == "John_Smith" & df$State == "WI", "Name"] <- "John_Smith1"

Another way using the dplyr:

df %>% mutate(Name = ifelse(State == "WI" & Name == "John_Smith", "John_Smith1", Name))

Note: As David Arenburg says, the first column should not be a factor. For this, reading the data set stringsAsFactors = FALSE.

3

You can also use package data.table:

library(data.table)
setDT(df)[State=="WI", Name:=paste0(Name,"1")]
1
  • 1
    This code will also rename Jeff Smith to Jeff Smith1. Use setDT(df)[State == "WI" & Name == "John Smith", Name := paste0(Name, "1")] to rename only John Smith from WI.
    – Uwe
    Jan 22, 2018 at 0:59
1

As the OP has mentioned that he has "a very big data frame", it might be advantageous to use a binary search

library(data.table)
setDT(DF)[.("John Smith",  "WI"), on = .(Name=V1, State=V2), 
          Name := paste0(Name, 1)][]
          Name State
1:  John Smith    MI
2: John Smith1    WI
3:  Jeff Smith    WI

instead of a vector scan

setDT(df)[State == "WI" & Name == "John Smith", Name := paste0(Name, "1")]

In both variations the data object is updated by reference, i.e., without copying the whole object which save time and memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.