Update a Value in One Column Based on Criteria in Other Columns

前端 未结 4 689
遥遥无期
遥遥无期 2020-11-29 05:28

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

相关标签:
4条回答
  • 2020-11-29 05:41

    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.

    0 讨论(0)
  • 2020-11-29 05:57

    You can also use package data.table:

    library(data.table)
    setDT(df)[State=="WI", Name:=paste0(Name,"1")]
    
    0 讨论(0)
  • 2020-11-29 05:59
    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'
    }) 
    
    0 讨论(0)
  • 2020-11-29 06:02

    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.

    0 讨论(0)
提交回复
热议问题