使用特定条件从后续行检索/替换列值

时间:2017-05-03 12:50:49

标签: r

我尝试每行在每天列值等于1时用下一个相应的返回值替换/更新结果列值。例如:

*可重现的例子:

A

*预期输出:

set.seed(123)
df<-data.frame(return=sample(runif(10, min = 0, max = 1)),day=seq(5, 1, by=-1), result =0)
df

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

例如,使用dplyr

library(dplyr)
df %>% 
  mutate(group = cumsum(lag(day, default = 0) == 1)) %>% 
  group_by(group) %>% 
  mutate(result = return[day == 1]) %>% 
  ungroup()

# # A tibble: 10 × 4
#       return   day    result group
#        <dbl> <dbl>     <dbl> <int>
# 1  0.4566147     5 0.2875775     0
# 2  0.9404673     4 0.2875775     0
# 3  0.0455565     3 0.2875775     0
# 4  0.5514350     2 0.2875775     0
# 5  0.2875775     1 0.2875775     0
# 6  0.5281055     5 0.7883051     1
# 7  0.8924190     4 0.7883051     1
# 8  0.8830174     3 0.7883051     1
# 9  0.4089769     2 0.7883051     1
# 10 0.7883051     1 0.7883051     1

答案 1 :(得分:0)

data.frame解决方案:

df$result <- df[df$day == 1, "return"][cumsum(lag(df$day, default = 0) == 1) + 1]
df
      return day    result
1  0.4566147   5 0.2875775
2  0.9404673   4 0.2875775
3  0.0455565   3 0.2875775
4  0.5514350   2 0.2875775
5  0.2875775   1 0.2875775
6  0.5281055   5 0.7883051
7  0.8924190   4 0.7883051
8  0.8830174   3 0.7883051
9  0.4089769   2 0.7883051
10 0.7883051   1 0.7883051

答案 2 :(得分:0)

数据表方法,

library(data.table)

setDT(df)[, result := return[day == 1], by = (grp =cumsum(c(1, diff(day != 1) == 1)))][]

#       return day    result
# 1: 0.4566147   5 0.2875775
# 2: 0.9404673   4 0.2875775
# 3: 0.0455565   3 0.2875775
# 4: 0.5514350   2 0.2875775
# 5: 0.2875775   1 0.2875775
# 6: 0.5281055   5 0.7883051
# 7: 0.8924190   4 0.7883051
# 8: 0.8830174   3 0.7883051
# 9: 0.4089769   2 0.7883051
#10: 0.7883051   1 0.7883051