0

I am trying to write my own function. And after some calculations for example I am obtaining a list like this ;

obtained list And according to data, number of clusters can vary from 1 to 31.

So, no matter how much the clusters are, I want to list them like the code below.

maxm5<-list(m.5$`Disaggregated rainfall depths`$`Cluster 1`, m.5$`Disaggregated rainfall depths`$`Cluster 2`...)

To perform these, I tried sapply;

maxm5<-sapply(1:31, function(zz) list(m.5$`Disaggregated rainfall depths`$`Cluster [zz]`))

And then I tried for loop

        month<-31 
   maxm5<- for (i in month) {
      list(m.5$`Disaggregated rainfall depths`$`Cluster [i]`)
    }

But what I just got is a list with 31 null.

And then I want to give name them like;

m5.1<-maxm5[[1]]
m5.2<-maxm5[[2]] ....
  • `for (i in month)` iterates over the vector `month`, which in this case is only the number 31. You probably want something like `month <- 1:31` – camille Apr 03 '19 at 18:27
  • @camille with `month <- 1:31`, I am getting `maxm5` as 1 line null – Hüsamettin Tayşi Apr 03 '19 at 18:33
  • ```m.5$`Disaggregated rainfall depths` ``` returns the list containing all the clusters. Is this not what you want? – josemz Apr 03 '19 at 18:54
  • @josemz Actually it is, but I need to find maximums of each cluster. And when I write `max(m.5$`Disaggregated rainfall depths`)` I am getting error. So I tried to find maximums seperately – Hüsamettin Tayşi Apr 03 '19 at 18:59
  • Oh my bad, didn't notice the max, how about: ```sapply(m.5$`Disaggregated rainfall depths`, max)``` – josemz Apr 03 '19 at 19:01
  • @josemz Oh thanks it worked. But there is another trouble. The clusters consist of `year`, `daily`, `season` and `values`. So when the year is 2005, normally as maximum `2005` is found. What can I do to find only `values`' maximum? – Hüsamettin Tayşi Apr 03 '19 at 19:06
  • ```sapply(m.5$`Disaggregated rainfall depths`, function(x) max(x$values))``` – josemz Apr 03 '19 at 19:08
  • @josemz I am so sorry, I explained wrong. https://i.stack.imgur.com/FpcFK.jpg This a cluster sample. It has day, season, year, daily and hour_1, hour_2.. and it is going until hour_288. So except day, season, year and daily, I need to find a maximum from rest. It doesn't matter which column or row is. – Hüsamettin Tayşi Apr 03 '19 at 19:13
  • We can't replicate this [without a sample of data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – camille Apr 03 '19 at 19:15

1 Answers1

1

Based on your last comment:

sapply(m.5$`Disaggregated rainfall depths`, function(x) max(x[, -(1:4)]))
josemz
  • 1,283
  • 7
  • 15