And*_*man 9 r dplyr data.table
好的,这是我想用dplyr实现的全部视图:
使用dplyr我正在进行计算以形成新列.
initial.capital -
x.long.shares -
x.end.value -
x.net.profit -
new.initial.capital
Run Code Online (Sandbox Code Playgroud)
执行此操作的代码:
# 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(x.long == 0,0,
ifelse(row_number() == n(),
first(inital.capital) / first(close.x),0))) %>%
dplyr::mutate(x.end.value = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
last(x.long.shares) * last(close.x),0))) %>%
dplyr::mutate(x.net.profit = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
last(initial.capital) - last(x.end.value),0))) %>%
dplyr::mutate(new.initial.capital = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
last(x.net.profit) + last(inital.capital),0))) %>%
ungroup() %>%
select(-RunID)
Run Code Online (Sandbox Code Playgroud)
我按x.long列分组.并在分组时.使用组内的第一个/最后一个位置从不同列进行计算我的基本问题是:
在照片中,请参阅new.initial.capital列下的红色突出显示.如何"保存"此值(10185.33)...并将其插入NEXT组,将其保存在initial.capital列下,再次以红色突出显示(它将替换10,000或将其存储在组的第一行) ?
我真正需要做的是将new.initial.capital列中的最终值保存到变量中.然后这个变量可以在下一组中使用(参见下面的代码)这里的值将用作下一组计算的一部分...然后当更新结束new.initial.capital时,这个值进入变量,然后它转到下一组的开头(见下面的代码)..然后所有的值将再次更新....变量将放在这里:
output <- df %>%
dplyr::mutate(RunID = data.table::rleid(x.long)) %>%
group_by(RunID) %>%
dplyr::mutate(x.long.shares = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
first(end_of_new.initial.capital_variable_from_previous_group) / first(close.x),0))) %>%
Run Code Online (Sandbox Code Playgroud)
我基本上想在dplyr组之间传递值.这可能吗?或者我每次都可以将它存储在变量中?
下面是照片中的一些示例数据:保存到.txt
df <- read.table("your_dir\df.txt",header=TRUE, sep="", stringsAsFactors=FALSE)
close.x x.long y.short x.short y.long inital.capital x.long.shares x.end.value x.net.profit new.initial.capital
37.96 NA NA NA NA 10000 NA NA NA NA
36.52 0 0 0 0 10000 0 0 0 0
38.32 0 0 0 0 10000 0 0 0 0
38.5504 0 0 0 0 10000 0 0 0 0
38.17 0 0 0 0 10000 0 0 0 0
38.85 1 1 0 0 10000 0 0 0 0
38.53 1 1 0 0 10000 0 0 0 0
39.13 1 1 0 0 10000 0 0 0 0
38.13 1 1 0 0 10000 257.4002574 9814.671815 185.3281853 10185.32819
37.01 0 0 1 1 10000 0 0 0 0
36.14 0 0 1 1 10000 0 0 0 0
35.27 0 0 1 1 10000 0 0 0 0
35.13 0 0 1 1 10000 0 0 0 0
32.2 0 0 1 1 10000 0 0 0 0
33.03 1 1 0 0 10000 0 0 0 0
34.94 1 1 0 0 10000 0 0 0 0
34.57 1 1 0 0 10000 0 0 0 0
33.6 1 1 0 0 10000 0 0 0 0
34.34 1 1 0 0 10000 302.7550711 10396.60914 -396.6091432 9603.390857
35.86 0 0 1 1 10000 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
我试着做一个变量:
inital.capital <- 10000
Run Code Online (Sandbox Code Playgroud)
并在代码中插入...
output <- df %>%
dplyr::mutate(RunID = data.table::rleid(x.long)) %>%
group_by(RunID) %>%
dplyr::mutate(x.long.shares = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
initial.capital / first(close.x),0))) %>% # place initial.capital variable.. initialized with 10000
dplyr::mutate(x.end.value = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
last(x.long.shares) * last(close.x),0))) %>%
dplyr::mutate(x.net.profit = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
last(initial.capital) - last(x.end.value),0))) %>%
dplyr::mutate(new.initial.capital = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
last(x.net.profit) + last(inital.capital),0))) %>%
dplyr::mutate(new.initial.capitals = ifelse(x.long == 0,0,
ifelse(row_number() == n(),
inital.capital < - last(new.initial.capital),0))) %>% # update variable with the final balance of new.inital.capital column
ungroup() %>%
select(-RunID)
Run Code Online (Sandbox Code Playgroud)
如果我每次都可以更新initial.capital变量.然后,这将作为组之间的"链接".但是,这个想法目前还没有在dplyr设置中工作.
任何协助赞赏.
Mat*_*wle 11
你在问题中使用data.table并标记了data.table这个问题,所以这里是一个data.table答案.在进行j求值时,它处于静态范围内,其中局部变量保留其来自前一组的值.
使用虚拟数据来演示:
require(data.table)
set.seed(1)
DT = data.table( long = rep(c(0,1,0,1),each=3),
val = sample(5,12,replace=TRUE))
DT
long val
1: 0 2
2: 0 2
3: 0 3
4: 1 5
5: 1 2
6: 1 5
7: 0 5
8: 0 4
9: 0 4
10: 1 1
11: 1 2
12: 1 1
DT[, v1:=sum(val), by=rleid(long)][]
long val v1
1: 0 2 7
2: 0 2 7
3: 0 3 7
4: 1 5 12
5: 1 2 12
6: 1 5 12
7: 0 5 13
8: 0 4 13
9: 0 4 13
10: 1 1 4
11: 1 2 4
12: 1 1 4
Run Code Online (Sandbox Code Playgroud)
到目前为止,足够简单.
prev = NA # initialize previous group value
DT[, v2:={ans<-last(val)/prev; prev<-sum(val); ans}, by=rleid(long)][]
long val v1 v2
1: 0 2 7 NA
2: 0 2 7 NA
3: 0 3 7 NA
4: 1 5 12 0.71428571
5: 1 2 12 0.71428571
6: 1 5 12 0.71428571
7: 0 5 13 0.33333333
8: 0 4 13 0.33333333
9: 0 4 13 0.33333333
10: 1 1 4 0.07692308
11: 1 2 4 0.07692308
12: 1 1 4 0.07692308
> 3/NA
[1] NA
> 5/7
[1] 0.7142857
> 4/12
[1] 0.3333333
> 1/13
[1] 0.07692308
> prev
[1] NA
Run Code Online (Sandbox Code Playgroud)
请注意,该prev值未更新,因为prev并且ans是在j每个组运行时更新的范围内的局部变量.为了说明,prev可以使用R的<<-运算符从每个组内更新全局:
DT[, v2:={ans<-last(val)/prev; prev<<-sum(val); ans}, by=rleid(long)]
prev
[1] 4
Run Code Online (Sandbox Code Playgroud)
但是没有必要<<-在data.table中使用,因为局部变量是静态的(保留它们之前组的值).除非您在查询完成后需要使用最终组的值.