R.K. R.K. - 2 months ago 4x
R Question

Deleting column with the least sum in dataframes dynamically in R

In a data frame, I am trying to delete the column whose sum is the least. I want it to be dynamic since I want to use it in a function


a b c
1 434 0 45
2 5452 1 456
3 42342 0 26
4 542 1 15
5 542 1 323
6 413 0 45

I want to remove the 2nd column [i.e. column b] since its sum is the least, but this I want it to be done dynamically since I have to use it as a part of a function


We can try with colSums with which.min to create the index of the minimum column sum and remove that column.


Or another option is Filter

mn <- min(sapply(df1, sum))
Filter(function(x) sum(x) != mn, df1)