generate column values with multiple conditions in R

后端 未结 8 2049
甜味超标
甜味超标 2020-12-29 00:22

I have a dataframe z and I want to create the new column based on the values of two old columns of z. Following is the process:

&g         


        
8条回答
  •  感动是毒
    2020-12-29 00:56

    Here's a version of an SQL decode in R for character vectors (untested with factors) that operates just like the SQL version. i.e. it takes an arbitrary number of target/replacement pairs, and optional last argument that acts as a default value (note that the default won't overwrite NAs).

    I can see it being pretty useful in conjunction with dplyr's mutate operation.

    > x <- c("apple","apple","orange","pear","pear",NA)
    
    > decode(x, apple, banana)
    [1] "banana" "banana" "orange" "pear"   "pear"   NA      
    
    > decode(x, apple, banana, fruit)
    [1] "banana" "banana" "fruit"  "fruit"  "fruit"  NA      
    
    > decode(x, apple, banana, pear, passionfruit)
    [1] "banana"       "banana"       "orange"       "passionfruit" "passionfruit" NA            
    
    > decode(x, apple, banana, pear, passionfruit, fruit)
    [1] "banana"       "banana"       "fruit"        "passionfruit" "passionfruit" NA  
    

    Here's the code I'm using, with a gist I'll keep up to date here (link).

    decode <- function(x, ...) {
    
      args <- as.character((eval(substitute(alist(...))))
    
      replacements <- args[1:length(args) %% 2 == 0]
      targets      <- args[1:length(args) %% 2 == 1][1:length(replacements)]
    
      if(length(args) %% 2 == 1)
        x[! x %in% targets & ! is.na(x)] <- tail(args,1)
    
      for(i in 1:length(targets))
        x <- ifelse(x == targets[i], replacements[i], x)
    
      return(x)
    
    }
    

提交回复
热议问题