[R] Help with a for complex for loop that looks into another data frame

Yan Wu yannikin at gmail.com
Fri Nov 7 22:54:11 CET 2014


Hello everyone!

I am working on a stock trading algorithm. I have created a data frame with
the stocks, and a summary data frame, master_df_ex, and master_df_ex_sum,
respectively.

The goal is to create something each day that has equal long and shorts,
and thus for each day, the sum of the ls_flag = 0.

For master_df_ex, there is a rank by strength of magnitude column, up or
down column, then a long or short column.

The ls_flag is set by the updn_flag for the stocks that are ranked in the
top half (in this case, since we have 4 per day, rank = 1 and rank = 2 are
set by updn_flag.

*I need to create a process to fill in the NA's in the ls_flag.*

My thinking is to create a summary table, where i run a summary,replace the
contents, until both the NA are 0 and the sum of the ls_flag =0.

For example, here is the current data:

asof_dt<-rep(seq(as.Date("2014-10-01"), as.Date("2014-10-03"), "days"),4)

rank_mag<-(rep(seq(1,4),3))

updn_flag<-c(-1,-1,-1,1,-1,-1,1,1,-1,1,-1,-1)

ls_flag<-c(-1,-1,NA,NA,-1,-1,NA,NA,-1,1,NA,NA)

master_df_ex<-data.frame(asof_dt,rank_mag,updn_flag,ls_flag)

master_df_ex<-arrange(master_df_ex,asof_dt,rank_mag)

master_df_ex_sum<-summarise(master_df,tot_flag = sum(ls_flag, na.rm = TRUE),

                        tot_NA = sum(is.na(ls_flag)))

> master_df_ex

      asof_dt rank_mag updn_flag ls_flag

1  2014-10-01        1        -1      -1

2  2014-10-01        2         1       1

3  2014-10-01        3         1      NA

4  2014-10-01        4         1      NA

5  2014-10-02        1        -1      -1

6  2014-10-02        2        -1      -1

7  2014-10-02        3        -1      NA

8  2014-10-02        4         1      NA

9  2014-10-03        1         1       1

10 2014-10-03        2         1       1

11 2014-10-03        3         1      NA

12 2014-10-03        4        -1      NA

> master_df_ex_sum

Source: local data frame [3 x 3]


 asof_dt tot_flag tot_NA

1 2014-10-01        0      2

2 2014-10-02       -2      2

3 2014-10-03        2      2

For 2014-10-02, since the the tot_flag = -2, the NA's for this date should
both be -1

For 2014-10-03, since the tot_flag = 2, the NA's should both be -1

For 2014-10-01 (hardest one):

The logic should look at 2014-10-01 in master_df_ex_sum, since tot_NA is
not 0, go into master_df_ex and find the lowest rank by rank_mag where
ls_flag is NA. Assign ls_flag = 1.

Then run the summary again, the NAs will be 1, and the sum of ls_flag will
be 1.

Then it should go into master_df_ex again, and assign a -1 to line 4. Then
the summary will have 0 and 0 and this date should be done.

I hope that makes sense! Any help is appreciated! Thank you very much.

-- 
Yan Wu
510-333-3188 <http://bigkidsbighearts.org>
yannikin at gmail.com

	[[alternative HTML version deleted]]



More information about the R-help mailing list