英文:
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 <- 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
<sup>Created on 2023-02-23 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论