0

I want to loop through some dataframes to find the max value in their respective first columns.

  1. All dataframes have the same column names. (Say, 'col1' 'col2')
  2. All dataframes have similar names (Say, 'file1' 'file2' 'file3')
  3. All columns are numerical

Below is a dummy code.

The max() function returns a value of type String instead of Numeric. In other words, it returns file1$col1, instead of the maximum number that corresponds to file1$col1.

allTheMax <- matrix(nrow=3, ncol=1) #DF to put my max values in.

for(i in 1:3){
  tempName <- paste("file",i,"$col1", sep="")
  allTheMax[i,1] <- max(tempName)
}

Instead of getting a numeric value as the max, R returns the string I concatenated using the 'paste' function.

  • 1
    You never read in your data.frame. You always return the max of the string, which only consists of one entry. How are your data.frames stored? In a list? In .csv files? As data.frames within your environments? – dasds Oct 10 '19 at 15:09
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 10 '19 at 15:09
  • Because that is exactly what you told it to do. You need to tell it to find the maximum of what you want. So far the way the code is set up, it will find the maximum of just one string,and therefore it will return the string given to it. What are you trying to achieve? – Onyambu Oct 10 '19 at 15:10
  • `sapply(mget(c('file1', 'file2', 'file3')), function(DF) max(DF[[1]]))`. – Rui Barradas Oct 10 '19 at 15:19

0 Answers0