根据条件添加值

时间:2017-06-02 07:53:16

标签: r

我有一个数据集

                         field_pointpath    Value    Expected
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    22098 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22100    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22100    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    18916
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18917    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18917    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17434    17434
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17441    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17441    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17446    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17475    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17488    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17504    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    93       NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    126      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    157      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    211      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    212      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    212      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    246      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    246      NA

如果field_pointpath中的expected的下一个值小于它应该在expected列中执行添加的值,那么是否有任何方式可以使用 field_pointpath Value Expected C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098 22098 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098 22098 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098 22098 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098 22098 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22100 22100 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22100 22100 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 0 22100 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 0 22100 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 0 22100 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 2 22102 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 2 22104 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 2 22106 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 2 22108 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 2 22110 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916 18916 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916 18916 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916 18916 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916 18916 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18917 18917 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18917 18917 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 0 18917 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 0 18917 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 14 18931 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 0 18931 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 5 18936 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 7 18943 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 9 18952 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 10 18962 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17434 17434 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17441 17441 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17441 17441 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17446 17446 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17475 17475 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17488 17488 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17504 17504 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 93 17597 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 126 17723 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 157 17880 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 211 18091 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 212 18303 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 212 18515 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 246 18761 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 246 19007

以下是我期望的结果

field_pointpath

所以基本上我寻求一个解决方案,如果每个唯一value[i+1]<expected[i]遇到一个小于之前值的值,它应该将前一个值与下一个值相加。

如果value[i+1]+expected[i] unique(field_pointpath) Sub exportComments() Dim xlApp As Object Dim xlWB As Object Dim i As Integer, HeadingRow As Integer Dim objPara As Paragraph Dim objComment As Comment Dim strSection As String Dim strTemp Dim myRange As Range Set xlApp = CreateObject("Excel.Application") xlApp.Visible = True Set xlWB = xlApp.Workbooks.Add 'create a new workbook With xlWB.Worksheets(1) ' Create Heading HeadingRow = 1 .Cells(HeadingRow, 1).Formula = "Comment" .Cells(HeadingRow, 2).Formula = "Page" .Cells(HeadingRow, 3).Formula = "Paragraph" .Cells(HeadingRow, 4).Formula = "Commented part" .Cells(HeadingRow, 5).Formula = "Comment" .Cells(HeadingRow, 6).Formula = "Reviewer" .Cells(HeadingRow, 7).Formula = "Date" strSection = "preamble" 'all sections before "1." will be labeled as "preamble" strTemp = "preamble" If ActiveDocument.Comments.Count = 0 Then MsgBox ("No comments") Exit Sub End If For i = 1 To ActiveDocument.Comments.Count Set myRange = ActiveDocument.Comments(i).Scope strSection = ParentLevel(myRange.Paragraphs(1)) 'MsgBox strSection 'Comment line .Cells(i + HeadingRow, 1).Formula = ActiveDocument.Comments(i).Index 'Page number line .Cells(i + HeadingRow, 2).Formula = ActiveDocument.Comments(i).Reference.Information(wdActiveEndAdjustedPageNumber) 'Paragraph indicator line .Cells(i + HeadingRow, 3).Formula = ActiveDocument.Comments(i).Scope.Paragraphs(1) 'Commented part line .Cells(i + HeadingRow, 4).Formula = ActiveDocument.Comments(i).Scope.FormattedText 'Comment value line .Cells(i + HeadingRow, 5).Formula = ActiveDocument.Comments(i).Range 'Comment reviewer line .Cells(i + HeadingRow, 6).Formula = ActiveDocument.Comments(i).Author 'Comment date line .Cells(i + HeadingRow, 7).Formula = Format(ActiveDocument.Comments(i).Date, "dd/MM/yyyy") Next i End With Set xlWB = Nothing Set xlApp = Nothing End Sub Function ParentLevel(Para As Word.Paragraph) As String ' Finds the first paragraph of the current section Dim oSection As Section Dim iSection As Integer Dim lngPara As Long Dim oRng As Range, oPara As Range iSection = Para.Range.Information(wdActiveEndSectionNumber) Set oSection = ActiveDocument.Sections(iSection) Set oRng = oSection.Range For lngPara = 1 To oRng.Paragraphs.Count Set oPara = oRng.Paragraphs(lngPara).Range If Len(oPara) > 1 Then Exit For End If Next lngPara oPara.End = oPara.End - 1 ParentLevel = oPara.Text End Function arr_type_and_value[0] >> 1 value,arr_type_and_value[1]>> 2 value, arr_type_and_value[2] >> 3 value and arr_type_and_value[3] >> 4 value. 则复制相同的值

2 个答案:

答案 0 :(得分:2)

使用data.table,这也可以写成一行:

library(data.table)
DT[, new := cummax(Value) + cumsum((Value < cummax(Value)) * Value), field_pointpath][]

将返回:

                             field_pointpath Value Expected   new
 1: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098 22098
 2: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098 22098
 3: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098 22098
 4: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098 22098
 5: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22100    22100 22100
 6: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22100    22100 22100
 7: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0    22100 22100
 8: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0    22100 22100
 9: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0    22100 22100
10: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22102 22102
11: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22104 22104
12: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22106 22106
13: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22108 22108
14: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22110 22110
15: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916 18916
16: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916 18916
17: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916 18916
18: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916 18916
19: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18917    18917 18917
20: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18917    18917 18917
21: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917 18917
22: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917 18917
23: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    14    18931 18931
24: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18931 18931
25: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     5    18936 18936
26: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     7    18943 18943
27: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     9    18952 18952
28: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    10    18962 18962
29: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17434    17434 17434
30: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17441    17441 17441
31: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17441    17441 17441
32: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17446    17446 17446
33: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17475    17475 17475
34: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17488    17488 17488
35: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17504    17504 17504
36: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    93    17597 17597
37: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   126    17723 17723
38: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   157    17880 17880
39: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   211    18091 18091
40: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   212    18303 18303
41: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   212    18515 18515
42: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   246    18761 18761
43: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   246    19007 19007
                             field_pointpath Value Expected   new

数据

请注意,此处正在使用Q的第二个数据集,这些数据集在某些行中与第一个数据集不同(请参阅comment)。

DT <- structure(list(field_pointpath = c("C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", "C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", 
"C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC", "C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", 
"C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", "C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", 
"C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", "C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", 
"C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", "C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", 
"C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", "C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", 
"C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", "C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", 
"C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", "C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", 
"C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC", "C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC"
), Value = c(22098L, 22098L, 22098L, 22098L, 22100L, 22100L, 
0L, 0L, 0L, 2L, 2L, 2L, 2L, 2L, 18916L, 18916L, 18916L, 18916L, 
18917L, 18917L, 0L, 0L, 14L, 0L, 5L, 7L, 9L, 10L, 17434L, 17441L, 
17441L, 17446L, 17475L, 17488L, 17504L, 93L, 126L, 157L, 211L, 
212L, 212L, 246L, 246L), Expected = c(22098L, 22098L, 22098L, 
22098L, 22100L, 22100L, 22100L, 22100L, 22100L, 22102L, 22104L, 
22106L, 22108L, 22110L, 18916L, 18916L, 18916L, 18916L, 18917L, 
18917L, 18917L, 18917L, 18931L, 18931L, 18936L, 18943L, 18952L, 
18962L, 17434L, 17441L, 17441L, 17446L, 17475L, 17488L, 17504L, 
17597L, 17723L, 17880L, 18091L, 18303L, 18515L, 18761L, 19007L
)), .Names = c("field_pointpath", "Value", "Expected"), row.names = c(NA, 
-43L), class = "data.frame")
setDT(DT)

答案 1 :(得分:0)

此代码可以执行此操作:

dat <- read.table(header=TRUE, stringsAsFactors = FALSE, text=
'                         field_pointpath    Value    Expected
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    22098 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22100    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22100    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    18916
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18917    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18917    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17434    17434
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17441    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17441    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17446    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17475    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17488    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17504    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    93       NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    126      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    157      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    211      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    212      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    212      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    246      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    246      NA')

my.fill <- function(d) {
  d$Expected[is.na(d$Expected)] <- -1
  d$Expected <- cummax(pmax(d$Expected, d$Value))
  ind <- which(d$Value < d$Expected)
  d$Expected[ind] <- d$Expected[ind[1]-1] + cumsum(d$Value[ind])
  d
}

L <- split(dat, dat$field_pointpath)
Lnew <- lapply(L, my.fill)
do.call(rbind, Lnew)

结果:

                                                                     field_pointpath Value Expected
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.1  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.2  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.3  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.4  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.5  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22100    22100
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.6  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22100    22100
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.7  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0    22100
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.8  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0    22100
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.9  C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0    22100
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.10 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22102
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.11 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22104
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.12 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22106
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.13 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22108
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC.14 C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2    22110
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.15 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.16 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.17 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.18 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.19 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18917    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.20 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18917    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.21 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.22 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.23 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.24 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.25 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.26 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.27 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC.28 C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0    18917
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.29 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17434    17434
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.30 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17441    17441
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.31 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17441    17441
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.32 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17446    17446
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.33 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17475    17475
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.34 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17488    17488
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.35 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17504    17504
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.36 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    93    17597
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.37 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   126    17723
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.38 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   157    17880
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.39 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   211    18091
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.40 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   212    18303
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.41 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   212    18515
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.42 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   246    18761
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC.43 C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   246    19007

以下是data.table的版本:

library("data.table")
dat <- fread(
'                         field_pointpath    Value    Expected
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    22098 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22098    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22100    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    22100    NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA 
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC    2        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    18916
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18916    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18917    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    18917    NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC    0        NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17434    17434
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17441    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17441    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17446    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17475    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17488    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    17504    NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    93       NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    126      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    157      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    211      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    212      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    212      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    246      NA
C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    246      NA')

my.fill <- function(Value, Expected) {
  Expected[is.na(Expected)] <- -1
  Expected <- cummax(pmax(Expected, Value))
  ind <- which(Value < Expected)
  Expected[ind] <- Expected[ind[1]-1] + cumsum(Value[ind])
  Expected
}

dat[, E:=my.fill(Value, Expected), by=field_pointpath]

结果:

> dat
                             field_pointpath Value Expected     E
 1: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098    22098 22098
 2: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098       NA 22098
 3: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098       NA 22098
 4: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22098       NA 22098
 5: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22100       NA 22100
 6: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC 22100       NA 22100
 7: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0       NA 22100
 8: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0       NA 22100
 9: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     0       NA 22100
10: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2       NA 22102
11: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2       NA 22104
12: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2       NA 22106
13: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2       NA 22108
14: C_THDxL_0154x6KSFS_A03x6KSF_11xaP_ACTACC     2       NA 22110
15: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916    18916 18916
16: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916       NA 18916
17: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916       NA 18916
18: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18916       NA 18916
19: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18917       NA 18917
20: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC 18917       NA 18917
21: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0       NA 18917
22: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0       NA 18917
23: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0       NA 18917
24: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0       NA 18917
25: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0       NA 18917
26: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0       NA 18917
27: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0       NA 18917
28: C_THDxL_0154x6KSFS_B03x6KSF_11xaP_ACTACC     0       NA 18917
29: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17434    17434 17434
30: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17441       NA 17441
31: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17441       NA 17441
32: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17446       NA 17446
33: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17475       NA 17475
34: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17488       NA 17488
35: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC 17504       NA 17504
36: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC    93       NA 17597
37: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   126       NA 17723
38: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   157       NA 17880
39: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   211       NA 18091
40: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   212       NA 18303
41: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   212       NA 18515
42: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   246       NA 18761
43: C_THDxL_0154x8KLFS_A06x8KLF_26xaP_ACTACC   246       NA 19007
                             field_pointpath Value Expected     E