Find max values in R [duplicate] - r

This question already has answers here:
which.max ties method in R
(4 answers)
Closed 2 years ago.
I have the problem, now you see that:
x<-c(1,2,3,6,4,5,6)
y=(which.max(x))
print(y)
*The result is 4 because it is the position of element 6 (max value). But I want the result returned is 4 and 7.
How can I do that?

Try the below:
which(x==max(x))
[1] 4 7
If you have potential NA values, use
which(x==max(x, na.rm=T))

Related

Subtract values from vector [duplicate]

This question already has answers here:
What does the diff() function in R do? [closed]
(2 answers)
Closed 5 months ago.
I dont know what´s the posible way of creating a new vector from a previous one by subtraction
of the second element to the first one and doing it for all the values of the vector, by
applying Xi-X(i-1)
Use diff
> x <- c(5,2,3,4)
> diff(x)
[1] -3 1 1

Create a vector: c(1,2,4,2,2) and use rank variable to return c(1,2,5,2,2) [duplicate]

This question already has answers here:
Extending rank() "Olympic Style"
(2 answers)
R - Add row index to a data frame but handle ties with minimum rank
(3 answers)
Rank with Ties in R
(1 answer)
Closed 2 years ago.
I would like to create a vector:
c(1,2,4,2,2)
And use rank variable to return:
c(1,2,5,2,2)
How do I use rank?
Check out the ?rank help page for options, specifically the ties.method= paramter. Specifically the output you want can be generated with ties.method="min"
rank(c(1,2,4,2,2), ties.method = "min")
# [1] 1 2 5 2 2
Using min_rank from dplyr
library(dplyr)
min_rank(c(1,2,4,2,2))
#[1] 1 2 5 2 2

vector of historical highs of another vector [duplicate]

This question already has answers here:
Calculating the maximum of sub-vectors of a larger vector
(2 answers)
Closed 3 years ago.
I searched at the forums but I could not find an answer for this.
I am looking for a way to convert
c(1,3,4,2,7,12,6,8,15)
to
c(1,3,4,4,7,12,12,12,15) using no loops.
It can be defined as a vector of historical highs of another vector.
This is an example, my data length will be fairly long.
thanks a lot,
Emre
We can use cummax from base R
cummax(v1)
#[1] 1 3 4 4 7 12 12 12 15
data
v1 <- c(1,3,4,2,7,12,6,8,15)

Transform a vector to run to its max value without decreasing [duplicate]

This question already has answers here:
Calculating the maximum of sub-vectors of a larger vector
(2 answers)
Closed 5 years ago.
I am looking for a clever way to transform a vector into a version of itself which runs to its maximum value without ever decreasing, and which plateaus once it reaches its maximum.
So, for input like:
x <- c(1,2,6,3,2,9,8,4)
I'd like output like:
y <- c(1,2,6,6,6,9,9,9)
I find this easy to do with a loop, but I am trying to do this to 1000's of reasonably large vectors, so I'd really prefer a more efficient solution. Thank you in advance!
We can use cummax to do this
cummax(x)
#[1] 1 2 6 6 6 9 9 9

Delete vector elements in a vector [duplicate]

This question already has answers here:
How to tell what is in one vector and not another?
(6 answers)
Closed 5 years ago.
I try to do this in R:(for example)
let x = c(1,2,3,4,5,6,7,8) and y=c(1,2,8)
So
x[x!=y] = numeric(0) ????
I want to get as a result 3,4,5,6,7
Is there a practical way to do this?
Thanks
Use value matching %in% and remove the elements of x that are present in y
x[-which(x %in% y)]
#[1] 3 4 5 6 7

Resources