R - 将值替换为前一个值的开始和结束逐行选择

时间:2017-06-14 21:05:18

标签: r dataframe fill

R - Replace values starting in selected column by row类似,我想用行替换前一个非零值的起始列和结束列之间的任何零值。鉴于数据:

df <- structure(list(Mth1 = c(0L, 0L, 5L, 0L, 2L), 
                 Mth2 = c(2L, 3L, 2L, 2L, 0L),
                 Mth3 = c(0L, 2L, 0L, 0L, 3L), 
                 Mth4 = c(3L, 0L, 0L, 4L, 0L),
                 StartMth = c(2L, 2L, 1L, 2L, 1L),
                 EndMth = c(4L, 3L, 3L, 4L, 3L)),
            .Names = c("Mth1", "Mth2", "Mth3", "Mth4", "StartMth", "EndMth"), class = "data.frame", 
            row.names = c("1", "2", "3", "4", "5"))

> df
  Mth1 Mth2 Mth3 Mth4 StartMth EndMth
1    0    2    0    3        2      4
2    0    3    2    0        2      3
3    5    2    0    0        1      3
4    0    2    0    4        2      4
5    2    0    3    0        1      3

我想使用StartMth和EndMth中的值来确定替换发生的位置。所需的输出是:

> df1
  Mth1 Mth2 Mth3 Mth4
1    0    2    2    3
2    0    3    2    0
3    5    2    2    0
4    0    2    2    4
5    2    2    3    0

注意,指定的StartMth和EndMth列中的值将始终为非零,StartMth之前的值和EndMth之后的值将始终为零。在需要替换的StartMth和EndMth列之间可能存在零个,一个或多个零值。

编辑:实际的数据帧有更多的月份和行,并且会随着时间的推移而增长,因此需要一个高效且通用的解决方案。 StartMth和EndMth将始终是最后一列。

我怀疑解决方案涉及使用apply变体,可能会对NA进行中间更改,然后应用locf来填充现在缺少的值。

1 个答案:

答案 0 :(得分:1)

这似乎有效,但你可以查一下吗?

Sub BarGraph()
'
' BarGraph Macro
'

'
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$D$5")
ActiveChart.SetElement (msoElementPrimaryCategoryAxisTitleAdjacentToAxis)
ActiveChart.SetElement (msoElementPrimaryValueAxisTitleAdjacentToAxis)
ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Text = "Total Sales"        'This is the line that the failure starts at
Selection.Format.TextFrame2.TextRange.Characters.Text = "Total Sales"
With Selection.Format.TextFrame2.TextRange.Characters(1, 11).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 11).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 10
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Strike = msoNoStrike
End With
ActiveChart.Axes(xlCategory).AxisTitle.Select
ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Text = "Region"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Region"
With Selection.Format.TextFrame2.TextRange.Characters(1, 6).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 6).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 10
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Strike = msoNoStrike
End With
ActiveChart.ChartArea.Select
ActiveChart.ChartTitle.Select
ActiveChart.ChartTitle.Text = "Total Sales"
Selection.Format.TextFrame2.TextRange.Characters.Text = "Total Sales"
With Selection.Format.TextFrame2.TextRange.Characters(1, 11).ParagraphFormat
    .TextDirection = msoTextDirectionLeftToRight
    .Alignment = msoAlignCenter
End With
With Selection.Format.TextFrame2.TextRange.Characters(1, 11).Font
    .BaselineOffset = 0
    .Bold = msoFalse
    .NameComplexScript = "+mn-cs"
    .NameFarEast = "+mn-ea"
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(89, 89, 89)
    .Fill.Transparency = 0
    .Fill.Solid
    .Size = 14
    .Italic = msoFalse
    .Kerning = 12
    .Name = "+mn-lt"
    .UnderlineStyle = msoNoUnderline
    .Spacing = 0
    .Strike = msoNoStrike
End With
Range("K7").Select
End Sub
相关问题