Prince Modi - 1 year ago 64

R Question

I have a list x2 having two data frames, x and x1. Both have 4 columns: n,m,l and k. I want to select the data frame that has maximum last value for column k.

In the below example, I would like data frame 2nd to be selected because the last value in column K is greater than last value in column K for data frame 1.

`x <- data.frame(n = c(2, 13, 5),m = c(2, 23, 6),l = c(2, 33, 7),k = c(2, 43, 8))`

x1 <- data.frame((n = c(2, 3, 15),m = c(2, 3, 16),l = c(2, 3, 17),k = c(2, 3, 18))

x2<-list(x,x1)

Answer Source

```
x2[which.max(lapply(x2, function(x) tail(x$k, 1)))]
# [[1]]
# n m l k
# 1 2 2 2 2
# 2 3 3 3 3
# 3 15 16 17 18
```