Search code examples
rmaxsubtraction

subtract max of column from all entries in column R


I have a data frame that looks like:

kfm <- data.frame ("data_source"=c("kfm", "kfm", "kfm", "kfm"), 
               "1996"= c(56, 56, 56, 57), 
               "1997"=c(8, 8, 9, 7),
               "1998"= c(101, 102, 101, 105))

I want to subtract the max of each column from each of the year columns (in my data frame I have about 20 years). So if the max of 1996 is 57. So I want to subtract 57 from each of the 1996 entries. And the max for 1997 is 9, so I would want to subtract 9 from each of the 1997 entries. I want the resulting data frame to look like:

kfm_differences <- data.frame ("data_source"=c("kfm", "kfm", "kfm", "kfm"), 
               "1996"= c(-1, -1, -1, 0), 
               "1997"=c(-1, -1, 0, -2),
               "1998"= c(-4, -3, -4, 0))

So that each entry now shows the difference between the max and the entry. I'm not sure how to go about this. I feel like I need to use apply() somehow, but I'm not quite sure.

Thanks!


Solution

  • Seems simple.

    kfm_differences <- kfm
    kfm_differences[-1] <- lapply(kfm[-1], function(x) x - max(x))
    

    By the way, when creating data frames with column names that start with numerals, use argument check.names = FALSE, otherwise R will append a X.