根据相同列上的条件替换行

huangapple go评论66阅读模式
英文:

Replacing rows based on conditions on the same column

问题

在上面的数据框中,我想要将所有"Status"为'FanOnly'的行替换为'Compressor',但前一行的"Status"为'Compressor'时才替换。对于那些最后一行不是'Compressor'的情况,我不想将'FanOnly'替换为'Compressor'。请注意,'Power'是数值,'Status'是字符。

以下是期望的数据框:

Power Status
1257 Compressor
1689 Compressor
897 Compressor
753 Compressor
514 Compressor
151 Compressor
77 Compressor
49 Compressor
16 Controls
15 Controls
77 FanOnly
67 FanOnly
90 FanOnly
111 FanOnly

可以考虑只根据"Status"列或同时考虑"Power"和"Status"列进行替换。

非常感谢帮助。我尝试了lag、lead以及论坛上的其他示例,但我发现很难获得期望的输出。

英文:

I am trying to replace the rows based on the conditions of the previous rows. Please note the column is character not numbers.

Power Status
1257 Compressor
1689 Compressor
897 Compressor
753 Compressor
514 Compressor
151 Compressor
77 FanOnly
49 FanOnly
16 Controls
15 Controls
77 FanOnly
67 FanOnly
90 FanOnly
111 FanOnly

In the above data frame, I would like to replace all the rows with the Status of 'FanOnly' with 'Compressor', if the previous row is 'Compressor'. There are several occasions when the last row is not 'Compressor'; for those occasions, I do not want to replace 'FanOnly' with 'Compressor. Please note 'Power' is numeric and 'Status' is a character.

The following is the desired data frame,

Power Status
1257 Compressor
1689 Compressor
897 Compressor
753 Compressor
514 Compressor
151 Compressor
77 Compressor
49 Compressor
16 Controls
15 Controls
77 FanOnly
67 FanOnly
90 FanOnly
111 FanOnly

The replacement could be made considering the 'Status' column alone or both the 'Power' & 'Status' columns.

Many thanks for the help.

I tried lag, lead and other examples from the forum, but I am finding difficulty in obtaining the desired output.

答案1

得分: 1

在基础的R语言中,我们可以使用循环来实现这个操作:


dt$Status_change <- dt$Status

for(i in 2:nrow(dt))
  dt$Status_change[i] <- ifelse(dt$Status_change[i] == "FanOnly" && dt$Status_change[i - 1] == "Compressor",
                         "Compressor", 
                         dt$Status_change[i])

dt
#>    Compressor_Power     Status Status_change
#> 1              1257 Compressor    Compressor
#> 2              1689 Compressor    Compressor
#> 3               897 Compressor    Compressor
#> 4               753 Compressor    Compressor
#> 5               514 Compressor    Compressor
#> 6               151 Compressor    Compressor
#> 7                77    FanOnly    Compressor
#> 8                49    FanOnly    Compressor
#> 9                16   Controls      Controls
#> 10               15   Controls      Controls
#> 11               77    FanOnly       FanOnly
#> 12               67    FanOnly       FanOnly
#> 13               90    FanOnly       FanOnly
#> 14              111    FanOnly       FanOnly

创建于2023-02-23,使用 reprex v2.0.2

英文:

in base R, we can do it with a loop:


dt$Status_change &lt;- dt$Status

for(i in 2:nrow(dt))
  dt$Status_change[i] &lt;- ifelse(dt$Status_change[i] == &quot;FanOnly&quot; &amp;&amp; dt$Status_change[i - 1] == &quot;Compressor&quot;,
                         &quot;Compressor&quot;, 
                         dt$Status_change[i])

dt
#&gt;    Compressor_Power     Status Status_change
#&gt; 1              1257 Compressor    Compressor
#&gt; 2              1689 Compressor    Compressor
#&gt; 3               897 Compressor    Compressor
#&gt; 4               753 Compressor    Compressor
#&gt; 5               514 Compressor    Compressor
#&gt; 6               151 Compressor    Compressor
#&gt; 7                77    FanOnly    Compressor
#&gt; 8                49    FanOnly    Compressor
#&gt; 9                16   Controls      Controls
#&gt; 10               15   Controls      Controls
#&gt; 11               77    FanOnly       FanOnly
#&gt; 12               67    FanOnly       FanOnly
#&gt; 13               90    FanOnly       FanOnly
#&gt; 14              111    FanOnly       FanOnly

<sup>Created on 2023-02-23 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年2月24日 00:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定