Hüsamettin Tayşi
Hüsamettin Tayşi

Reputation: 574

apply any function to elements which returned from which.max function in r

I want to apply any function to elements which I found its location with which.max function. For example, my sample data is below:

 $Apr
$Apr$`04-2036`
         date value
92 04-01-2036  0.00
93 04-02-2036  3.13
94 04-03-2036 20.64

$Apr$`04-2037`
          date value
457 04-01-2037  5.32
458 04-02-2037 82.47
459 04-03-2037 15.56


$Dec
$Dec$`04-2039`
           date value
1431 12-01-2039     3
1432 12-02-2039     0
1433 12-03-2039    11

$Dec$`04-2064`
            date value
10563 12-01-2064     0
10564 12-02-2064     5
10565 12-03-2064     0





data<-structure(list(Apr = structure(list(`04-2036` = structure(list(
    date = c("04-01-2036", "04-02-2036", "04-03-2036"), value = c(0, 
    3.13, 20.64)), .Names = c("date", "value"), row.names = 92:94, class = "data.frame"), 
    `04-2037` = structure(list(date = c("04-01-2037", "04-02-2037", 
    "04-03-2037"), value = c(5.32, 82.47, 15.56)), .Names = c("date", 
    "value"), row.names = 457:459, class = "data.frame")), .Names = c("04-2036", 
"04-2037")), Dec = structure(list(`04-2039` = structure(list(
    date = c("12-01-2039", "12-02-2039", "12-03-2039"), value = c(3, 
    0, 11)), .Names = c("date", "value"), row.names = 1431:1433, class = "data.frame"), 
    `04-2064` = structure(list(date = c("12-01-2064", "12-02-2064", 
    "12-03-2064"), value = c(0, 5, 0)), .Names = c("date", "value"
    ), row.names = 10563:10565, class = "data.frame")), .Names = c("04-2039", 
"04-2064"))), .Names = c("Apr", "Dec"))

I have found locations of maximum values for each element in lists of list using the functions below.

drop<-function(y){
  lapply(y, function(x)(x[!(names(x) %in% c("date"))]))
}

q1<-lapply(data, drop)
q2<-lapply(q1, function(x) unlist(x,recursive = FALSE))
daily_max<-lapply(q2, function(x) lapply(x, max))
dailymax <- data.frame(matrix(unlist(daily_max), nrow=length(daily_max), byrow=TRUE))
row.names(dailymax)<-names(daily_max)
apply(dailymax, 1, which.max)

Locations of max. values of each element is computed as it is seen below;

Apr Dec 
  2   1 

Now, I want to apply any function to these elements automatically for all my data(it is Apr 2 = Apr$04-2037 and Dec$2039).

Upvotes: 0

Views: 365

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

You can subset and keep only the max value data in each list.

max_value <- apply(dailymax, 1, which.max)
Map(`[[`, data, max_value)

#$Apr
#          date value
#457 04-01-2037  5.32
#458 04-02-2037 82.47
#459 04-03-2037 15.56

#$Dec
#           date value
#1431 12-01-2039     3
#1432 12-02-2039     0
#1433 12-03-2039    11

Let's say you want to apply the function fn to this list.

fn <- function(x) {x$value <- x$value * 2;x}

You can change the Map function as -

Map(function(x, y) fn(x[[y]]), data, max_value)

#$Apr
#          date  value
#457 04-01-2037  10.64
#458 04-02-2037 164.94
#459 04-03-2037  31.12

#$Dec
#           date value
#1431 12-01-2039     6
#1432 12-02-2039     0
#1433 12-03-2039    22

Upvotes: 2

Related Questions