maximusdooku - 15 days ago 5x

R Question

I have a data frame

`df<-structure(list(time = structure(c(1080868500, 1080868800, 1080869100,`

1080869400, 1080869700, 1080870000, 1080870300, 1080870600, 1080870900,

1080871200, 1080871500, 1080871800, 1080872100, 1080872400, 1080872700,

1080873000, 1080873300, 1080873600, 1080873900, 1080874200, 1080874500,

1080874800, 1080875100, 1080875400, 1080875700, 1080876000, 1080876300,

1080876600, 1080876900, 1080877200, 1080877500, 1080877800, 1080878100,

1080878400, 1080878700, 1080879000, 1080879300, 1080879600, 1080879900,

1080880200, 1080880500, 1080880800, 1080881100, 1080881400, 1080881700,

1080882000, 1080882300, 1080882600, 1080882900, 1080883200), class = c("POSIXct",

"POSIXt"), tzone = "UTC"), precip = c(1.76, 1.76, 1.21, 0.78,

0.59, 0.59, 0.62, 0.62, 0.81, 0.81, 1.14, 0.82, 0.87, 1.03, 0.98,

0.77, 0.77, 0.45, 0.55, 0.82, 0.8, 0.58, 0.7, 0.7, 1.03, 1.25,

1.32, 1.68, 2.6, 1.49, 3.85, 3.91, 2.94, 3.63, 4.12, 1.85, 2.02,

3.46, 3.45, 2.53, 2.88, 3, 2.42, 1.56, 1.44, 1.43, 1.33, 1.27,

1.35, 1.4)), .Names = c("time", "precip"), row.names = 236752:236801, class = "data.frame")

I want to find the maximum value that exists between rows 10 and 20. But I want to find the original index of that maxim value.

`which.max(df[10:20,]$precip)`

gives me the index of 2. I know I can add it to row index 10. But is there a proper way to do it?

Answer

It is simply `(10:20)[2]`

.

Now consider a more complicated case:

```
set.seed(0)
index <- sample(1:nrow(df), 10) ## a random subset of size 10
pos <- which.max(df[index,"precip"]) ## position in the subset data frame
# [1] 5
index[pos] ## position in the original data frame
# [1] 42
```

Thanks. Can you explain why this is a more complicated case? Looks the same as my case.

In your case where `index = 10:20`

, which is consecutive and sorted increasingly, you may add `10 + 2 = 12`

. But in my case where `index`

is not consecutive (even not sorted):

```
# [1] 45 14 18 27 42 10 40 41 28 26
```

there is no way you can do an addition to get row number in original data frame.

Source (Stackoverflow)

Comments