Dataframe create new column based on other columns

后端 未结 5 435
既然无缘
既然无缘 2020-12-05 15:33

I have a dataframe:

df <- data.frame(\'a\'=c(1,2,3,4,5), \'b\'=c(1,20,3,4,50))
df
    a    b
1   1    1
2   2   20
3   3    3
4   4    4
5   5   50


        
相关标签:
5条回答
  • 2020-12-05 16:09

    Using dplyr package:

    library(dplyr)
    
    df <- df %>% 
      mutate(c = if_else(a == b, a + b, b - a))
    
    df
    #   a  b  c
    # 1 1  1  2
    # 2 2 20 18
    # 3 3  3  6
    # 4 4  4  8
    # 5 5 50 45
    
    0 讨论(0)
  • 2020-12-05 16:10

    One option is ifelse which is vectorized version of if/else. If we are doing this for each row, the if/else as showed in the OP's pandas post can be done in either a for loop or lapply/sapply, but that would be inefficient in R.

    df <- transform(df, c= ifelse(a==b, a+b, b-a))
    df
    #  a  b  c
    #1 1  1  2
    #2 2 20 18
    #3 3  3  6
    #4 4  4  8
    #5 5 50 45
    

    This can be otherwise written as

    df$c <- with(df, ifelse(a==b, a+b, b-a))
    

    to create the 'c' column in the original dataset


    As the OP wants a similar option in R using if/else

    df$c <- apply(df, 1, FUN = function(x) if(x[1]==x[2]) x[1]+x[2] else x[2]-x[1])
    
    0 讨论(0)
  • 2020-12-05 16:10

    Here is a slightly more confusing algebraic method:

    df$c <- with(df, b + ((-1)^((a==b)+1) * a))
    
    df
      a  b  c
    1 1  1  2
    2 2 20 18
    3 3  3  6
    4 4  4  8
    5 5 50 45
    

    The idea is that the "minus" operator is turned on or off based on the test a==b.

    0 讨论(0)
  • 2020-12-05 16:15

    A solution with apply

    myFunction <- function(x){
      a <- x[1]
      b <- x[2]
      #further values ignored (if there are more than 2 columns)
      value <- if(a==b) a + b else b - a
      #or more complicated stuff
      return(value)
    }
    
    df$c <- apply(df, 1, myFunction)
    
    0 讨论(0)
  • 2020-12-05 16:20

    If you want an apply method, then another way with mapply would be create a function and apply it,

    fun1 <- function(x, y) if (x == y) {x + y} else {y-x}
    df$c <- mapply(fun1, df$a, df$b)
    df
    #  a  b  c
    #1 1  1  2
    #2 2 20 18
    #3 3  3  6
    #4 4  4  8
    #5 5 50 45
    
    0 讨论(0)
提交回复
热议问题