Raj Raina
Raj Raina

Reputation: 99

R: Find max/min of list of columns in data frame

Suppose I have a list of column names as a vector:

vec=c("C1" , "C2" ,"C3").

I know for a fact that these column names come from a data frame df:

df:

C1 C2 C3 C4 C5
1   2  3  4  5
1   4  3  5  6
3   2  4  1  3

How can I find the maximum value of each column in vec, looking at their values in df. For example, something like:

boostedMax(vec, df, na.rm=T)

Obviously that doesnt work, but the idea is that boostedMax takes a vector of column names and a data frame where the columns are, and returns the max values from these columns. In this example, it would return the vector:

(3,4,4)

Thanks in advance!

Upvotes: 4

Views: 10976

Answers (4)

akrun
akrun

Reputation: 887851

We can use colMaxs from matrixStats after converting the subset of dataset to matrix

library(matrixStats)
colMaxs(as.matrix(df[vec]))
#[1] 3 4 4

Or another option is dplyr

library(dplyr)
df %>%
    summarise_each_(funs(max), vec)
#  C1 C2 C3
#1  3  4  4

Upvotes: 1

Hack-R
Hack-R

Reputation: 23231

vec=c("C1" , "C2" ,"C3")

C1 C2 C3 C4 C5
1   2  3  4  5
1   4  3  5  6
3   2  4  1  3

df <- read.table(con<-file("clipboard"), header = T)
df

apply(df[,vec],2,max)
C1 C2 C3 
 3  4  4

or

for(i in vec){
  print(max(df[,i]))
}
[1] 3
[1] 4
[1] 4

or

sapply(df[,vec],max)
C1 C2 C3 
 3  4  4

If you think that some levels of vec may not be in df you can do df[,colnames(df) %in% vec] instead of df[,vec]

Upvotes: 3

Timo Kvamme
Timo Kvamme

Reputation: 2964

dat <- data.frame(a=c(1,2,3),b=c(2,3,4),c=c(3,4,5))
> dat
  a b c
1 1 2 3
2 2 3 4
3 3 4 5

> sapply(dat, max, na.rm = TRUE)
a b c 
3 4 5 

Upvotes: 0

Sathish
Sathish

Reputation: 12723

df <- data.frame(a = c(1:4, NA), b = 6:10, c = 11:15)
d <- colnames(df)[1:2]
sapply(df[d], max, na.rm = TRUE)

Upvotes: 1

Related Questions