Home > database >  R - extract minimum and maximum values
R - extract minimum and maximum values

Time:08-26

I have a list something like this:

 my_data<- list(c(dummy= 300), structure(123.7, .Names = ""), 
    structure(143, .Names = ""), structure(113.675, .Names = ""), 
    structure(163.75, .Names = ""), structure(656, .Names = ""), 
    structure(5642, .Names = ""), structure(1232, .Names = "")) 

I want the minimun and maximum values from this list

I have tried using

min(my_data) 
max(my_data)
But I get an error: Error in min(weighted_mae) : invalid 'type' (list) of argument

typeof(my_data) #[1] "list"
class(my_data) #[1] "list"

What is the right way for getting the minimum and maximum from my_data?

CodePudding user response:

You could do:

my_data |> 
        unlist(use.names = FALSE) |> 
        range()

The following is the same, without piping:

range(unlist(my_data, use.names = FALSE))

If you want to get minimum and maximum values separately, then you could do:

min(unlist(my_data, use.names = FALSE))

max(unlist(my_data, use.names = FALSE))
  •  Tags:  
  • r
  • Related