how to calculate cumsum with depreciation in a grouped dataframe?

Bayes

I tried to calculate the cumsum with a depreciation rate.

I have a grouped dataframe with a column number. I want to add the number one by one with depreciation. If the rate is 1, then the cumsum function in base r is good enough. But if not, let's say the rate of 0.5 (means each number will multiply by 0.5 to add the next number), cumsum is not enough. I tried to write my own function to work with dplyr, but it fails.

library(tidyverse)
# dataframe
id=sample(1:5,25,replace=TRUE)
num=rnorm(25)
df=data.frame(id,num)

# my custom function
depre=function(data){
    rate=0.5
    r=nrow(data)
    sl=data$num
    nl=data$num
    for (i in 2:r){
        sl[i]=sl[i-1]*rate+nl[i]
    }
    return(sl)
}

# work with one group
df %>% filter(id==1) %>% depre(.)

# failed to work with dplyr
df %>% group_by(id) %>% mutate(sl=depre(.))

I expect the first element of column s, should be the same as in column num. But the following ones, should be depreciate by times 0.5 and add next num. It works in one group, but failed in multi-grouped dataframe. The error message is: "Error: Column sl must be length 6 (the group size) or one, not 25". I have no idea. Could anyone have a clue? Thanks

Ronak Shah

Your function would work if you pass vector to your function instead of dataframe

depre <-  function(num){
    rate = 0.5
    r= length(num)
    sl = num
    nl =  num
    for (i in 2:r){
      sl[i]=sl[i-1]*rate+nl[i]
    }
    return(sl)
}

and then apply it by group.

library(dplyr)
df %>% group_by(id) %>% mutate(sl = depre(num))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to calculate mean of grouped dataframe?

From Dev

How to calculate RMS for dataframe column grouped by two column labels?

From Dev

Calculate medians of rows in a grouped dataframe

From Dev

Grouped diminishing weighted cumsum

From Dev

How to calculate difference on grouped df?

From Dev

Calculate quantile on grouped data in spark Dataframe

From Dev

Calculate random variables from grouped dataframe

From Dev

calculate sum of rows in pandas dataframe grouped by date

From Dev

A simpler way to calculate grouped percentages in a Spark dataframe?

From Dev

Pandas DataFrame: How to calculate the rolling mean of both grouped and shifted data in version >0.19

From Dev

cumsum on all non grouped vars

From Dev

How to filter dataframe by grouped value?

From Dev

How to disable depreciation in Android studio?

From Java

How to calculate percentiles grouped by column using partitionedBy?

From Mysql

How to calculate first occurrence of event, grouped by type?

From Dev

How to calculate the correlation coefficient of grouped quantities in Pandas?

From Dev

How to impute values derived from a grouped dataframe into another grouped dataframe?

From Dev

dataframe - how to perform actions on grouped dataframe

From Dev

R: Calculate distance between first and current row of grouped dataframe

From Dev

Calculate new column in pandas dataframe based only on grouped records

From Dev

How to calculate mean in a dataframe?

From Dev

How to calculate rowMeans for dataframe?

From Dev

How to get multiple aggregation in a dataframe? cumsum and count columns

From Dev

How to add new vector that includes 0 and cumsum to dataframe?

From Python

How to use the pandas.DataFrame.cumsum() function with a filter faster?

From Dev

How can I begin calculating cumsum() on a specific date within a dataframe?

From Dev

R dataframe with special cumsum

From Dev

How to create grouped barplot from dataframe grouped by two columns with intervals

From Dev

Function to calculate cumsum for multiple conditions

Related Related

  1. 1

    How to calculate mean of grouped dataframe?

  2. 2

    How to calculate RMS for dataframe column grouped by two column labels?

  3. 3

    Calculate medians of rows in a grouped dataframe

  4. 4

    Grouped diminishing weighted cumsum

  5. 5

    How to calculate difference on grouped df?

  6. 6

    Calculate quantile on grouped data in spark Dataframe

  7. 7

    Calculate random variables from grouped dataframe

  8. 8

    calculate sum of rows in pandas dataframe grouped by date

  9. 9

    A simpler way to calculate grouped percentages in a Spark dataframe?

  10. 10

    Pandas DataFrame: How to calculate the rolling mean of both grouped and shifted data in version >0.19

  11. 11

    cumsum on all non grouped vars

  12. 12

    How to filter dataframe by grouped value?

  13. 13

    How to disable depreciation in Android studio?

  14. 14

    How to calculate percentiles grouped by column using partitionedBy?

  15. 15

    How to calculate first occurrence of event, grouped by type?

  16. 16

    How to calculate the correlation coefficient of grouped quantities in Pandas?

  17. 17

    How to impute values derived from a grouped dataframe into another grouped dataframe?

  18. 18

    dataframe - how to perform actions on grouped dataframe

  19. 19

    R: Calculate distance between first and current row of grouped dataframe

  20. 20

    Calculate new column in pandas dataframe based only on grouped records

  21. 21

    How to calculate mean in a dataframe?

  22. 22

    How to calculate rowMeans for dataframe?

  23. 23

    How to get multiple aggregation in a dataframe? cumsum and count columns

  24. 24

    How to add new vector that includes 0 and cumsum to dataframe?

  25. 25

    How to use the pandas.DataFrame.cumsum() function with a filter faster?

  26. 26

    How can I begin calculating cumsum() on a specific date within a dataframe?

  27. 27

    R dataframe with special cumsum

  28. 28

    How to create grouped barplot from dataframe grouped by two columns with intervals

  29. 29

    Function to calculate cumsum for multiple conditions

HotTag

Archive