Search code examples
rdataframesubtraction

Subtracting columns of data frame by name


Let's assume I have a data frame as bellow:

df <- as.data.frame(matrix(seq(1,20,1),nrow=4), byrow=TRUE)
colnames(df) <- c("X1","X2","X3","X4","X5")
rownames(df) <- as.Date(c("2020-01-02","2020-01-03","2020-01-04","2020-01-05"))

df
           X1 X2 X3 X4 X5
2020-01-02  1  2  3  4  5
2020-01-03  6  7  8  9 10
2020-01-04 11 12 13 14 15
2020-01-05 16 17 18 19 20

I want to subtract all columns from the first column X1 and store it in the same column. I have tried doing

  for(i in colnames(df)){
    df[i] <- lapply(df[i], function(x) x-df["X1"])
  }

But it only applies it to the first column. How can I run it for all the columns?


Solution

  • Try this base R solution without loop. Just have in mind the position of columns:

    #Data
    df <- as.data.frame(matrix(seq(1,20,1),nrow=4), byrow=TRUE)
    colnames(df) <- c("X1","X2","X3","X4","X5")
    rownames(df) <- as.Date(c("2020-01-02","2020-01-03","2020-01-04","2020-01-05"))
    #Set columns for difference
    df[,2:5] <- df[,2:5]-df[,1]
    

    Output:

               X1 X2 X3 X4 X5
    2020-01-02  1  4  8 12 16
    2020-01-03  2  4  8 12 16
    2020-01-04  3  4  8 12 16
    2020-01-05  4  4  8 12 16
    

    Or a more sophisticated way would be:

    #Create index
    #Var to substract
    i1 <- which(names(df)=='X1')
    #Vars to be substracted with X1
    i2 <- which(names(df)!='X1')
    #Compute
    df[,i2]<-df[,i2]-df[,i1]
    

    Output:

               X1 X2 X3 X4 X5
    2020-01-02  1  4  8 12 16
    2020-01-03  2  4  8 12 16
    2020-01-04  3  4  8 12 16
    2020-01-05  4  4  8 12 16