mardi 31 juillet 2018

conditional statements through a loop R

  unique_ref priority_201801 balance_201801 action_201801 priority_201802 balance_201802 action_201802 priority_201803 balance_201803 action_201803
1          1               3             30   text,letter               1             60        letter               2             30          text
2          2               2            -20         visit               1            -40          text               2            -40          call
3          3               3             35        letter               1             35          call               3            -50          text
4          4               0           -100          call               2              0         visit               2            100          call
  priority_201804 balance_201804 action_201804
1              99              0          text
2               0            -20         visit
3               0            -50        letter
4               0           -100          text

Above is a sample of my dataset. I want to search through each action looking for the first instance of the action "text", this will then be Week0, I then want to line up the priority's and balances of the next weeks, as Week1, Week2 etc.

Desirable output:

  unique_ref week0_priority week0_balance week0_action week1_priority week1_balance week1_action week2_priority week2_balance week2_action
1          1              3            30         text              1            60       letter              2            30         text
2          2              1           -40         text              2           -40         call              0           -20        visit
3          3              3           -50         text              0           -40       letter        No Data       No Data      No Data
4          4              0          -100         text        No Data       No Data      No Data        No Data       No Data      No Data
  week3_priority week3_balance week3_action
1             99             0         text
2        No Data       No Data      No Data
3        No Data       No Data      No Data
4        No Data       No Data      No Data

Reproducible example of data:

unique_ref=c(1,2,3,4)
priority_201801=c('3','2','3','0')
balance_201801=c('30','-20','35','-100')
action_201801=c('text,letter','visit','letter','call')
priority_201802=c('1','1','1','2')
balance_201802=c('60','-40','35','0')
action_201802=c('letter','text', 'call', 'visit')
priority_201803=c('2','2','3','2')
balance_201803=c('30','-40','-50','100')
action_201803=c('text','call','text','call')
priority_201804=c('99','0','0','0')
balance_201804=c('0','-20','-50','-100')
action_201804=c('text','visit','letter','text')

df3=as.data.frame(cbind(unique_ref,priority_201801,balance_201801,action_201801,priority_201802,balance_201802,action_201802,priority_201803,
                        balance_201803,action_201803,priority_201804,balance_201804,action_201804))

Aucun commentaire:

Enregistrer un commentaire