Raj Raina Raj Raina - 4 months ago 7
R Question

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!

Answer
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]

Comments