How to apply custom function to vector accumulation in R? In an efficient and idiomatic way?


Alessandro Jacopson

I know a function in cumsumR that computes the cumulative sum of its vector arguments.

I need the "cumulative apply" not a summation function, but a "generic" function, which in my specific case is the quantilefunction.

My current solution is based on a loop:

set.seed(42)
df<-data.frame(measurement=rnorm(1000),upper=0,lower=0)
for ( r in seq(1,nrow(df))){
  df$upper[r]<-quantile(df[seq(1,r),"measurement"],c(.99))
  df$lower[r]<-quantile(df[seq(1,r),"measurement"],c(.01))
}

x=seq(1,nrow(df))
plot(df$measurement,type="l",col="grey")
lines(x,df$upper,col="red")
lines(x,df$lower,col="blue")

enter image description here

It works, but it's not very efficient and I think there should be a more idiomatic approach in R.

Sven Hornstein

You can use this method:

set.seed(42)
df <- data.frame(measurement = rnorm(1000))

res <- sapply(seq(nrow(df)), function(x) 
  quantile(df[seq(x), "measurement"], c(.01, .99)))

It creates a matrix with nrow(df)two and two rows , the first row is the first percentile, and the first row is the ninety-ninth percentile.

You can add this information to the dataframe df(as two columns):

df <- setNames(cbind(df, t(res)), c(names(df), "lower", "upper"))

Related


Apply custom accumulation function to pandas

Dragon I have the following dataframe and like to apply a custom cumulative formula to the columns. How should I put them in a function and apply it to the new column? thank you very much! Excel formula: new column =(previous period value + 1) * (1 + current p

Apply custom accumulation function to pandas

Dragon I have the following dataframe and like to apply a custom cumulative formula to the columns. How should I put them in a function and apply it to the new column? thank you very much! Excel formula: new column =(previous period value + 1) * (1 + current p

Apply custom accumulation function to pandas

Dragon I have the following dataframe and like to apply a custom cumulative formula to the columns. How should I put them in a function and apply it to the new column? thank you very much! Excel formula: new column =(previous period value + 1) * (1 + current p

Apply custom accumulation function to pandas

Dragon I have the following dataframe and like to apply a custom cumulative formula to the columns. How should I put them in a function and apply it to the new column? thank you very much! Excel formula: new column =(previous period value + 1) * (1 + current p

Apply custom accumulation function to pandas

Dragon I have the following dataframe and like to apply a custom cumulative formula to the columns. How should I put them in a function and apply it to the new column? thank you very much! Excel formula: new column =(previous period value + 1) * (1 + current p

Apply custom accumulation function to pandas

Dragon I have the following dataframe and like to apply a custom cumulative formula to the columns. How should I put them in a function and apply it to the new column? thank you very much! Excel formula: new column =(previous period value + 1) * (1 + current p

Apply custom accumulation function to pandas

Dragon I have the following dataframe and like to apply a custom cumulative formula to the columns. How should I put them in a function and apply it to the new column? thank you very much! Excel formula: new column =(previous period value + 1) * (1 + current p

How to apply a vector on a user defined function in R

Arjun Pankaj I have a user defined function make_datafor creating a dataset . I need to use make _dataand generate 3 different datasets mu_1 <- seq(1:3). I don't know how to use sapplybecause the make_datafunction has multiple parameters, library(dplyr) # for

How to apply a vector on a user defined function in R

Arjun Pankaj I have a user defined function make_datafor creating a dataset . I need to use make _dataand generate 3 different datasets mu_1 <- seq(1:3). I don't know how to use sapplybecause the make_datafunction has multiple parameters, library(dplyr) # for

How to apply a vector on a user defined function in R

Arjun Pankaj I have a user defined function make_datafor creating a dataset . I need to use make _dataand generate 3 different datasets mu_1 <- seq(1:3). I don't know how to use sapplybecause the make_datafunction has multiple parameters, library(dplyr) # for

How to apply a vector on a user defined function in R

Arjun Pankaj I have a user defined function make_datafor creating a dataset . I need to use make _dataand generate 3 different datasets mu_1 <- seq(1:3). I don't know how to use sapplybecause the make_datafunction has multiple parameters, library(dplyr) # for

How to apply a vector on a user defined function in R

Arjun Pankaj I have a user defined function make_datafor creating a dataset . I need to use make _dataand generate 3 different datasets mu_1 <- seq(1:3). I don't know how to use sapplybecause the make_datafunction has multiple parameters, library(dplyr) # for

How to apply a vector on a user defined function in R

Arjun Pankaj I have a user defined function make_datafor creating a dataset . I need to use make _dataand generate 3 different datasets mu_1 <- seq(1:3). I don't know how to use sapplybecause the make_datafunction has multiple parameters, library(dplyr) # for

Apply a function to a vector in R

MF14 I'm fairly new to programming with R and I'm wondering why this doesn't work: w <- c(1,0) deriv(~x^2+y,c("x","y"),function.arg = TRUE)(w) I really want to apply the function produced by deriv() to the variable w. Maybe some background on how to deal with

Apply a function to a vector in R

MF14 I'm fairly new to programming with R and I'm wondering why this doesn't work: w <- c(1,0) deriv(~x^2+y,c("x","y"),function.arg = TRUE)(w) I really want to apply the function produced by deriv() to the variable w. Maybe some background on how to deal with

Apply a function to a vector in R

MF14 I'm fairly new to programming with R and I'm wondering why this doesn't work: w <- c(1,0) deriv(~x^2+y,c("x","y"),function.arg = TRUE)(w) I really want to apply the function produced by deriv() to the variable w. Maybe some background on how to deal with

Apply a function to a vector in R

MF14 I'm fairly new to programming with R and I'm wondering why this doesn't work: w <- c(1,0) deriv(~x^2+y,c("x","y"),function.arg = TRUE)(w) I really want to apply the function produced by deriv() to the variable w. Maybe some background on how to deal with

R: custom function in apply()

Paul First time users are here! I'm just learning R and I'm hoping for an easy question. I have a number, array of numbers, and I will make sure that none of the numbers are greater than one. I am trying to do myfct <- function(x) { if ( x > 1.0 ) x = 1.0

R: custom function in apply()

Paul First time users are here! I'm just learning R and I'm hoping for an easy question. I have an array of numbers, numbers, and I will make sure that no number is greater than one. I am trying to do myfct <- function(x) { if ( x > 1.0 ) x = 1.0 return(x)

Efficient way to apply a function on a list of dataframes

John L_10 I have a list of dataframes in R. What I need to do is apply a function to each dataframe, in this case, remove special characters, and return a list of dataframes. Using lapplyand as.data.framethe following command works fine for my needs: my_df =da

Most efficient way to apply a function to a dataframe column

carousel I have a large dataframe full of lyrics for songs. I've labeled the lyrics column so each row is a list of lyrics, i.e. ["You", "say", "goodbye", "and", "I", "say", "hello"]so on. I wrote a function that uses a list of positive and negative words to c

Efficient way to apply a function to elements of a numpy array?

Leo I have a huge boolean 1D numpy array wand a growing list of indices idivided into wsub len(i)+1- arrays . An example of a toy is: w=numpy.array([True,False,False,False,True,True,True,True,False,False]) i=numpy.array([0,0,2,5,5,8,8]) I wish to compute a nu