-1

Okay, I am probably pursuing a roundabout way of accomplishing this, but I had a list called "goodAttributes" and a dataframe called QTable. If a column name of QTable is in "goodAttributes" than I want to return the max of that column, otherwise I want to return the minimum of the column...

I checked if the column name of QTable was in the list "goodAttributes" using "grepl" and then reset the column names of QTable if the value was in the list "goodAttributes" as "True" and otherwise set the column name as "False", and called the new table QTable2.

Current DataFrame called "QTableB":

enter image description here

Now, if the column name of QTableB is "True" I want to find the maximum of that column, and if the column name of QTableB is "False" I want to return the minimum of that column... storing the results in a new data frame.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    Edit your question. (Don't use comments.) Post data as text and then show your code. Not my downvote. (Somebody else saw the same problems and was lazy.) – IRTFM Jan 27 '17 at 18:58

1 Answers1

0

One way to do this is to simply apply a function to all of the colnames of the frame:

QTable  <- data.frame(v1=1:10,v2=11:20,v3=31:40)
goodAttributes <- c("v1","v3")

sapply(colnames(QTable),function(c){
  if(c %in% goodAttributes) max(QTable[,c])
  else min(QTable[,c])})

which yields

v1 v2 v3 
10 11 40

i.e. the max of v1 and v3 (the column names in goodAttributes) and the min of v2.