samedi 24 avril 2021

For loop with conditions in R

I have a list in R which contains 7 dates, and I want to search for those dates in another dataframe and change the value of another column. My list is ("2015-03-29" "2016-03-27" "2017-03-26" "2018-03-25" "2019-03-31" "2020-03-29" "2021-03-28") The data frame is:

df

date          time     time_prog    value1   value2
2015-01-01    0:00             1      4532       334      
2015-01-01    0:10             1      4589       485
2015-01-01    0:20             1      4232       324      
2015-01-01    0:30             1      4689       345
2015-01-01    0:40             1      4572       354      
2015-01-01    0:50             1      4129       455
2015-01-01    1:00             2      3332       324      
2015-01-01    1:10             2      3589       475
2015-01-01    1:30             2      5532       384      
2015-01-01    1:40             2      6589       685
2015-01-01    1:50             2      4552       534      
2015-01-01    2:00             3      4549       475
2015-01-01    2:10             3      4232       384      
2015-01-01    2:20             3      4289       455
.
.
.
omitted 331363 rows

My data is all days since 2015/01/01 until today, so for sure the dates on the list are in the column date.

This is the part in my dataframe that I want to change


df

date          time     time_prog    value1   value2
2015-03-29    1:00             2      4532       334      
2015-03-29    1:10             2      4589       485
2015-03-29    1:20             2      4232       324      
2015-03-29    1:30             2      4689       345
2015-03-29    1:40             2      4572       354      
2015-03-29    1:50             2      4129       455
2015-03-29    3:00             4      3332       324      
2015-03-29    3:10             4      3589       475
2015-03-29    3:20             4      5532       384      
2015-03-29    3:30             4      6589       685
2015-03-29    3:30             4      4652       594      
2015-03-29    3:50             4      4552       534 
2015-03-29    4:00             5      4549       475

Where it shows a 4 in column time_prog i would like to see a 3, and the same for the next hours, rest 1 to all the next values but JUST FOR THAT DAY, at the end of the day it should end like this


df

date          time     time_prog    value1   value2
2015-03-29    23:00            23      4532       334      
2015-03-29    23:10            23      4589       485
2015-03-29    23:20             23     4232       324      
2015-03-29    23:30            23      4689       345
2015-03-29    23:40            23      4572       354      
2015-03-29    23:50            23     4129       455
2015-03-30    0:00             1      3332       324      
2015-03-30    0:10             1      3589       475
2015-03-30    0:20             1      5532       384      
2015-03-30    0:30             1      3332       324      
2015-03-30    0:40             1      3589       475
2015-03-30    0:50             1      5532       384   
2015-03-30    1:00             2      3332       324      
2015-03-30    1:10              2     3589       475
2015-03-30    1:20              2     5532       384   

And what I receive doing loop is this


df

date          time     time_prog    value1   value2
2015-03-29    23:00            23      4532       334      
2015-03-29    23:10            23      4589       485
2015-03-29    23:20             23     4232       324      
2015-03-29    23:30            23      4689       345
2015-03-29    23:40            23      4572       354      
2015-03-29    23:50            23     4129       455
2015-03-30    0:00             0      3332       324      
2015-03-30    0:10             0      3589       475
2015-03-30    0:20             0      5532       384      
2015-03-30    0:30             0      3332       324      
2015-03-30    0:40             0      3589       475
2015-03-30    0:50             0      5532       384   
2015-03-30    1:00             1      3332       324      
2015-03-30    1:10              1     3589       475
2015-03-30    1:20              1     5532       384   

I hope you understand what I want, and it just have to be aplied on the 7 dates which are on the list, because those are the dates when hour is changed and there is one hour less because at 2 o’clock it changes to 3 o’clock automatically. Thank you for your time!

Aucun commentaire:

Enregistrer un commentaire