vector of historical highs of another vector [duplicate] - r

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)

Related

Adding successive values in a column of a csv in R [duplicate]

This question already has answers here:
Calculating cumulative sum for each row
(6 answers)
Closed 5 months ago.
I am new to coding so this is probably very basic. I was wondering how to add two successive values in a column of a csv along its entire length. Say this was my data:
[1] 2
[2] 3
[3] 4
[4] 5
I want to make a vector which contains 2+3, 3+4 and 4+5 (but obviously my real data set is much larger).
Thanks a lot!
I think you need cumsum():
If this is your vector:
vector <- 2:5
[1] 2 3 4 5
You would need to:
cumsum(vector)
[1] 2 5 9 14

R - how to create sequence [duplicate]

This question already has answers here:
Create a vector that contains the first 10 powers of 2, then the first 10 powers of 3 by R language
(2 answers)
Closed 1 year ago.
How can I create this sequence in R?
I've been watching some guides but these are simple sequences.
It is vectorized. We can do
2^(1:10)/(1:10)

Find max values in R [duplicate]

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))

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

Need to convert a column or data to 0 or 1 in R [duplicate]

This question already has answers here:
Generate a dummy-variable
(17 answers)
Closed 6 years ago.
I am doing an assignment with the built-in "SWISS" dataset, and where the value is greater than 50 I need to create a binary variable taking 1
I have a feeling that I might have to use the subset command, but I am lost as how to implement it.
using dplyr
swiss %>%
mutate(aggGreaterThan50 = (Agriculture > 50) * 1)

Resources