R 列,取决于其他列的名称

R 列,取决于其他列的名称,r,R,我有以下数据: year V2 V3 V4 V5 V6 V7 1 1 1 8 2000000 78888 135555556 2 1 1 8 2111111 16888 0 3 1 1 8 2222222 12777 0 4 1 1 8 2333333 12222 0 5 1 1 8 1444444 77777 0 6 1 1 8 5555555 26666 0 7 1 1

我有以下数据:

year V2 V3 V4 V5       V6     V7
1    1  1  8  2000000  78888  135555556
2    1  1  8  2111111  16888  0
3    1  1  8  2222222  12777  0
4    1  1  8  2333333  12222  0
5    1  1  8  1444444  77777  0
6    1  1  8  5555555  26666  0
7    1  1  8  2111111  22222  0
8    1  1  8  2666666  34444  0
如果
year>1
,则
v7
应等于前一年的
v7-v6
。这应该针对
v2
v3
v4
的每组进行

因此:

year V2 V3 V4 V5       V6     V7
1    1  1  8  2000000  78888  135555556
2    1  1  8  2111111  16888  135476668
3    1  1  8  2222222  12777  135459780
4    1  1  8  2333333  12222  135447003
5    1  1  8  1444444  77777  135434781
6    1  1  8  5555555  26666  135357004
7    1  1  8  2111111  22222  135330338
8    1  1  8  2666666  34444  135308116

如何构造生成该输出的函数?

这里有一个使用
cumsum
head
的解决方案

df$V7 <- head(df$V7, 1) - c(0, head(cumsum(df$V6), -1))
如果需要按组执行此操作(不在示例中),可以使用split-apply-combine方法

do.call(c, lapply(split(df, df[c("V2", "V3", "V4")]),
                  function(x) {head(x$V7, 1) - c(0, head(cumsum(x$V6), -1))}))
这假设每组V2和V4从第1年开始

数据

df <- 
structure(list(year = 1:8, V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), V3 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V4 = c(8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L), V5 = c(2000000L, 2111111L, 2222222L, 2333333L, 
1444444L, 5555555L, 2111111L, 2666666L), V6 = c(78888L, 16888L, 
12777L, 12222L, 77777L, 26666L, 22222L, 34444L), V7 = c(135555556L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("year", "V2", "V3", 
"V4", "V5", "V6", "V7"), class = "data.frame", row.names = c(NA, 
-8L))

df这里有一个使用
cumsum
head
的解决方案

df$V7 <- head(df$V7, 1) - c(0, head(cumsum(df$V6), -1))
如果需要按组执行此操作(不在示例中),可以使用split-apply-combine方法

do.call(c, lapply(split(df, df[c("V2", "V3", "V4")]),
                  function(x) {head(x$V7, 1) - c(0, head(cumsum(x$V6), -1))}))
这假设每组V2和V4从第1年开始

数据

df <- 
structure(list(year = 1:8, V2 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), V3 = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), V4 = c(8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L), V5 = c(2000000L, 2111111L, 2222222L, 2333333L, 
1444444L, 5555555L, 2111111L, 2666666L), V6 = c(78888L, 16888L, 
12777L, 12222L, 77777L, 26666L, 22222L, 34444L), V7 = c(135555556L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("year", "V2", "V3", 
"V4", "V5", "V6", "V7"), class = "data.frame", row.names = c(NA, 
-8L))
df
for(2中的i:长度(df$V7)){
如果(df$年[i]>1){
df$V7[i]
for(2中的i:长度(df$V7)){
如果(df$年[i]>1){

df$V7[i]我们可以使用
tidyverse
方法

library(tidyverse)
df %>% 
    mutate(V7=first(V7)- cumsum(lag(V6, default = 0)))
#  year V2 V3 V4      V5    V6        V7
#1    1  1  1  8 2000000 78888 135555556
#2    2  1  1  8 2111111 16888 135476668
#3    3  1  1  8 2222222 12777 135459780
#4    4  1  1  8 2333333 12222 135447003
#5    5  1  1  8 1444444 77777 135434781
#6    6  1  1  8 5555555 26666 135357004
#7    7  1  1  8 2111111 22222 135330338
#8    8  1  1  8 2666666 34444 135308116
如果我们需要分组的话

df %>%
   group_by(.dots = names(df)[2:4]) %>%
   mutate(V7=first(V7)- cumsum(lag(V6, default = 0)))

我们可以使用
tidyverse
方法

library(tidyverse)
df %>% 
    mutate(V7=first(V7)- cumsum(lag(V6, default = 0)))
#  year V2 V3 V4      V5    V6        V7
#1    1  1  1  8 2000000 78888 135555556
#2    2  1  1  8 2111111 16888 135476668
#3    3  1  1  8 2222222 12777 135459780
#4    4  1  1  8 2333333 12222 135447003
#5    5  1  1  8 1444444 77777 135434781
#6    6  1  1  8 5555555 26666 135357004
#7    7  1  1  8 2111111 22222 135330338
#8    8  1  1  8 2666666 34444 135308116
如果我们需要分组的话

df %>%
   group_by(.dots = names(df)[2:4]) %>%
   mutate(V7=first(V7)- cumsum(lag(V6, default = 0)))

你试图解释你想为
v7
做什么的部分没有意义。你能更清楚地理解你的意思吗?你试图解释你想为
v7
做什么的部分没有意义。你能更清楚地理解你的意思吗。