data.table

Cumulative sum from a month ago until the current day for all the rows

痞子三分冷 提交于 2020-12-30 03:55:11
问题 I have a data.table with ID, dates and values like the following one: DT <- setDT(data.frame(ContractID= c(1,1,1,2,2), Date = c("2018-02-01", "2018-02-20", "2018-03-12", "2018-02-01", "2018-02-12"), Value = c(10,20,30,10,20))) ContractID Date Value 1: 1 2018-02-01 10 2: 1 2018-02-20 20 3: 1 2018-03-12 30 4: 2 2018-02-01 10 5: 2 2018-02-12 20 I'd like to get a new column with the total cumulative sum per ID from a month ago until the current day for each row, like in the table below. NB: the

Cumulative sum from a month ago until the current day for all the rows

拜拜、爱过 提交于 2020-12-30 03:54:15
问题 I have a data.table with ID, dates and values like the following one: DT <- setDT(data.frame(ContractID= c(1,1,1,2,2), Date = c("2018-02-01", "2018-02-20", "2018-03-12", "2018-02-01", "2018-02-12"), Value = c(10,20,30,10,20))) ContractID Date Value 1: 1 2018-02-01 10 2: 1 2018-02-20 20 3: 1 2018-03-12 30 4: 2 2018-02-01 10 5: 2 2018-02-12 20 I'd like to get a new column with the total cumulative sum per ID from a month ago until the current day for each row, like in the table below. NB: the

A complicated sum in R data.table that involves looking at other columns

空扰寡人 提交于 2020-12-30 02:20:04
问题 I have a data table where each value for variables v1 and v2 has an associated "type", coded in a separate column. Here is a MWE: X <- data.table(id = 1:5, group = c(1,1,2,2,2), v1 = c(10,12,14,16,18), type_v1 = c("t1","t2","t1","t1","t2"), v2 = c(3,NA,NA,7,8), type_v2 = c("t2", "", "", "t3","t3")) print(X) id group v1 type_v1 v2 type_v2 1: 1 1 10 t1 3 t2 2: 2 1 12 t2 NA 3: 3 2 14 t1 NA 4: 4 2 16 t1 7 t3 5: 5 2 18 t2 8 t3 I want to sum up the values in columns v1 and v2 for each type by the

A complicated sum in R data.table that involves looking at other columns

前提是你 提交于 2020-12-30 02:18:05
问题 I have a data table where each value for variables v1 and v2 has an associated "type", coded in a separate column. Here is a MWE: X <- data.table(id = 1:5, group = c(1,1,2,2,2), v1 = c(10,12,14,16,18), type_v1 = c("t1","t2","t1","t1","t2"), v2 = c(3,NA,NA,7,8), type_v2 = c("t2", "", "", "t3","t3")) print(X) id group v1 type_v1 v2 type_v2 1: 1 1 10 t1 3 t2 2: 2 1 12 t2 NA 3: 3 2 14 t1 NA 4: 4 2 16 t1 7 t3 5: 5 2 18 t2 8 t3 I want to sum up the values in columns v1 and v2 for each type by the

data.table: Subset and find cumulative product for each row

时光总嘲笑我的痴心妄想 提交于 2020-12-25 05:37:09
问题 I have a simple dataframe containing three columns: An id, a date and a value. Now, I want to calculate a new value, newValue, based on these three columns following this procedure: For each row (i.e., for each pair of (id, date)) For all dates in the range (date, date+2) I want to find the cumulative product of the values of that id (and then subtract 1) The simple example below with made-up numbers does the computation: df <- data.frame("id"=rep(1:10, 5), "date"=c(rep(2000, 10), rep(2001,

dplyr group by, carry forward value from previous group to next

断了今生、忘了曾经 提交于 2020-12-24 23:58:27
问题 Ok this is the over all view of what i'm trying to achieve with dplyr: Using dplyr I am making calculations to form new columns. initial.capital - x.long.shares - x.end.value - x.net.profit - new.initial.capital The code that does this: # Calculate Share Prices For Each ETF # Initialize Start Capital Column library(dplyr) library(data.table) df$inital.capital <- 10000 output <- df %>% dplyr::mutate(RunID = data.table::rleid(x.long)) %>% group_by(RunID) %>% dplyr::mutate(x.long.shares = ifelse

dplyr group by, carry forward value from previous group to next

生来就可爱ヽ(ⅴ<●) 提交于 2020-12-24 23:53:19
问题 Ok this is the over all view of what i'm trying to achieve with dplyr: Using dplyr I am making calculations to form new columns. initial.capital - x.long.shares - x.end.value - x.net.profit - new.initial.capital The code that does this: # Calculate Share Prices For Each ETF # Initialize Start Capital Column library(dplyr) library(data.table) df$inital.capital <- 10000 output <- df %>% dplyr::mutate(RunID = data.table::rleid(x.long)) %>% group_by(RunID) %>% dplyr::mutate(x.long.shares = ifelse

dplyr group by, carry forward value from previous group to next

半城伤御伤魂 提交于 2020-12-24 23:51:58
问题 Ok this is the over all view of what i'm trying to achieve with dplyr: Using dplyr I am making calculations to form new columns. initial.capital - x.long.shares - x.end.value - x.net.profit - new.initial.capital The code that does this: # Calculate Share Prices For Each ETF # Initialize Start Capital Column library(dplyr) library(data.table) df$inital.capital <- 10000 output <- df %>% dplyr::mutate(RunID = data.table::rleid(x.long)) %>% group_by(RunID) %>% dplyr::mutate(x.long.shares = ifelse

R cbinding lists of data.tables

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-15 04:42:40
问题 I have two lists of data.tables of equal length in R. I want to element-wise cbind them. I sort of want to do a zip function where the input is two lists and the output is one list as illustrated below: list1 list2 list3 ----- ----- ---------------- dt1a dt2a cbind(dt1a,dt2a) dt1b dt2b => cbind(dt1b,dt2b) dt1c dt2c cbind(dt1c,dt2c) What is the easiest way to do this? 回答1: list3 <- mapply(cbind, list1, list2, SIMPLIFY=FALSE) 来源: https://stackoverflow.com/questions/48233811/r-cbinding-lists-of

R cbinding lists of data.tables

谁都会走 提交于 2020-12-15 04:40:47
问题 I have two lists of data.tables of equal length in R. I want to element-wise cbind them. I sort of want to do a zip function where the input is two lists and the output is one list as illustrated below: list1 list2 list3 ----- ----- ---------------- dt1a dt2a cbind(dt1a,dt2a) dt1b dt2b => cbind(dt1b,dt2b) dt1c dt2c cbind(dt1c,dt2c) What is the easiest way to do this? 回答1: list3 <- mapply(cbind, list1, list2, SIMPLIFY=FALSE) 来源: https://stackoverflow.com/questions/48233811/r-cbinding-lists-of