0

I keep obtaining this error when running my code:

check_transactions <- function(flags, trans_col, industry_column, industry_df){
  for(i in seq(1:length(flags$individual_id))){
    if(flags$industry_column[i]>0 && nrow(subset(industry_df, industry_df$individual_id == flags$individual_id[i]))!=0){
      flags$trans_col[i] <- subset(industry_df, industry_df$individual_id == ind_id)['txn_num']
    }
  }
}

check_transactions(flags1, txn_num_ind01, ind01_flag, ind1)

Error in if (flags$industry_column[i] > 0 && nrow(subset(industry_df,  : 
  missing value where TRUE/FALSE needed

After searching other posts, the general consensus is that there are NA's in the data, which messes with the logical. This is not the case for my data, I have no missing values.

When I test the condition for a specified 'i', I get a TRUE or FALSE, so I'm not sure why my loop is not working.

Am I missing something? Here is a snippet of my two data frames:
flags1 :

  individual_id ind01_flag ind05_flag ind06_flag ind07_flag txn_num_ind01 txn_num_ind05 txn_num_ind06 txn_num_ind07
1      10000230          2          2          2          2             0             0             0             0
2      10002230          2          1          1          2             0             0             0             0
3      10002380          2          2          2          2             0             0             0             0
4      10003040          1          2          1          2             0             0             0             0
5      10003260          1          2          0          1             0             0             0             0
6      10007230          2          1          0          1             0             0             0             0

and ind1

  individual_id txn_num
1      10000075      42
2      10000178     110
3      10000230     134
4      10000407      49
5      10000447     131
6      10000677     114
CAK
  • 23
  • 4
  • Read the documentation for `subset`, specifically the *Warning* section. – Rich Scriven Nov 07 '16 at 18:18
  • I'm not sure I see where I'm going wrong with the subset. When I subset the data frame using the same code, outside the loop, I obtain the desired output. – CAK Nov 07 '16 at 18:23
  • Looking a the warning section, I changed it to: nrow(industry_df[industry_df$individual_id == flags$individual_id[i],])!=0 This did not change the error. Is this what you meant? – CAK Nov 07 '16 at 18:27
  • 2
    " ind01_flag" is a column name which should be passed as a character vector and then subsetted using []. similarily for "txn_num_ind01". e.g.flag["txn_num_ind01"] – joel.wilson Nov 07 '16 at 18:27

0 Answers0