On command to find out the maximum quantity from the list

I have a list like this:

Ll 
$a
3.4 5.6 -2.1 -7.8
$b
2.1 6.7
$c
-6.7,0.001,8.9

I want to find out the maximum number for all elements of the list, regardless of the signs. those. I want my appearance to look like this:

Ll
$a
-7.8
$b
6.7
$c
8.9

Is there a way to do this using a single command line? Can this be done with the "by" command?

+5
source share
3 answers

Reproducible code / data always helps:

L1 <- list(a = c(3.4, 5.6, -2.1, -7.8), b = c(2.1, 6.7), c = c(-6.7, 0.001, 8.9))

Use lapplyto apply your own function to each element, which.maxit easily finds the maximum, and we get only olta absin each of them:

lapply(L1, function(x) x[which.max(abs(x))])
$a
[1] -7.8

$b
[1] 6.7

$c
[1] 8.9
+11
source

lapply - your friend!

eg.

.list <- list( a = 1:5, b = runif(7), c = -3:1)
 lapply(.list, function(x) x[which.max(abs(x))])
## $a
## [1] 5
## 
## $b
## [1] 0.9248526
## 
## $c
## [1] -3
+1
source

which.max , , , :

max (sapply ( , max))

0

All Articles