Home > Net >  how to get maximum/minimum values from each dataframe of a list in R
how to get maximum/minimum values from each dataframe of a list in R

Time:11-20

I have a list with multiple dataframes say 30,000 and in each dataframe i have multiple columns. the sample list with three dataframes is as follows

df1 <- data.frame(ID = c('a', 'a', 'a', 'a','a', 'a'), a = c('a','b','c','d','e','f'), b = 
c(0,1,2,3,0,5), c= c(11,3,2,4,0,'NA'), d=c(0,2,5,7,'NA',5), e = c(0,5,'NA',3,0,'NA'), f = 
c(14,7,4,3,'NA',7), g = c(1,2,3,4,5,6), h = c(10,2,13,4,5,6))

df2 <- data.frame(ID = c('b', 'b', 'b', 'b','b', 'b'), a = c('a','b','c','d','e','f'), b = 
c(0,1,2,3,0,5), c= c(11,3,2,4,0,'NA'), d=c(0,2,15,7,'NA',5), e = c(0,5,'NA',3,0,'NA'), f = 
c(14,7,4,3,'NA',7), g = c(1,2,3,4,5,6), h = c(10,2,13,4,5,6))

df3 <- data.frame(ID = c('c', 'c', 'c', 'c','c', 'c'), a = c('a','b','c','d','e','f'), b = 
c(0,1,2,3,0,5), c= c(11,3,2,4,0,'NA'), d=c(0,2,5,27,'NA',5), e = c(0,5,'NA',3,0,'NA'), f = 
c(14,7,4,3,'NA',7), g = c(1,2,3,4,5,6), h = c(10,2,13,4,5,6))

abc <- list (df1, df2, df3)

i would like to find out the maximum value of each dataframe in a list. The final desired output should be in a dataframe as follows

abc <- 

ID   Max.Value
'a'    14
'b'    15
'c'    27

I have tried the following codes

max(unlist(abc), na.rm = T)  

using the following code i am getting only maximum/minimum values

sapply(abc, function(x) max(x[3:9], na.rm=TRUE))

but i am looking for the desired output as mentioned above

CodePudding user response:

library(tidyverse)

bind_rows(abc)%>%
  group_by(ID)%>%
  type_convert()%>%
  summarise(max_value = max(across(where(is.numeric)), na.rm = TRUE))

# A tibble: 3 × 2
  ID    max_value
  <chr>     <dbl>
1 a            14
2 b            15
3 c            27

CodePudding user response:

One possible answer can be :

for (i in 1:length(abc))
{
  print(max( as.numeric(unlist(abc[[i]])), na.rm=TRUE))
}

or without do loop/package

  data.frame(
    ID=sapply(1:length(abc), FUN=function(x) abc[[x]][1,'ID']),
    Max.Value=sapply(1:length(abc), FUN= function(x)max( as.numeric(unlist(abc[[x]])), na.rm=TRUE)  )
  )
  • Related