I am looking to convert the given below loop into lapply. I'm very new to lapply concepts not 100 % sure how i can convert the loop into lapply
Right now with the current loop, R studio is aborting the session due to excess memory usage. I am already using 10GB Ram.
To provide sume statitics on resultset, for each loop, SQL fetches around 200k records in the memory. Based on the example, 2k *200k will be result set in the loop and hence it is getting crashed
for (cSegNbr in 1:nrow(segment_df)){
segnbr <- segment_df[cSegNbr, "segment_nbr"]
slsdecile <- segment_df[cSegNbr, "sales_decile"]
promodecile <- segment_df[cSegNbr, "sm_rate_decile"]
BPCdecile <- segment_df[cSegNbr, "bpc_sales_decile"]
HCdecile <- segment_df[cSegNbr, "hc_sales_decile"]
Otherdecile <- segment_df[cSegNbr, "other_sales_decile"]
# print(paste('select * from BUSINESS_USERS.cp_cntrl_pool_cohort_final_new_nl
# where cohort_month=201908 and segment_nbr=', segnbr,' and
# other_sales_decile=',Otherdecile,' and hc_sales_decile=',HCdecile,' and
# bpc_sales_decile=',BPCdecile,' and sales_decile=',slsdecile, ' and
# sm_rate_decile=',promodecile))
seg_df <- paste('select * from BUSINESS_USERS.cp_cntrl_pool_cohort_final_new_nl
where cohort_month=',year_list[1],' and segment_nbr=', segnbr,' and
other_sales_decile=',Otherdecile,' and hc_sales_decile=',HCdecile,' and
bpc_sales_decile=',BPCdecile,' and sales_decile=',slsdecile, ' and
sm_rate_decile=',promodecile)
seg_Nbr_final<-gsub("[\r\n]", "", seg_df)
query_seg <- dbSendQuery(IDW2, seg_Nbr_final)
df<-dbFetch(query_seg )
m.out= matchit(test_control ~
q1_sls+q2_sls+q2_sls+q4_sls+sls_nbr_of_wks+fs_sales_amt_m1+fs_sales_amt_m2+fs_sa
les_amt_m3+fs_sales_amt_m4+fs_sales_amt_m5+fs_sales_amt_m6+fs_sales_amt_m7+fs_sa
les_amt_m8+fs_sales_amt_m9+fs_sales_amt_m10+fs_sales_amt_m11+fs_sales_amt_m12+fs
_sales+avg_bskt_size+promo_depth+SB_penetration+personalized_cpn+base_cpn+mass_c
pn+phr_flag+bty_club_flag+card_tenure_years+cust_age,data=df, method="nearest",
ratio=1)
m.data1<-match.data(m.out)
df_total <- rbind(df_total,m.data1)
# print(paste('select * from BUSINESS_USERS.cp_cntrl_pool_cohort_final_new_nl
# where cohort_month=201908 and segment_nbr=', segnbr,' and
# sales_decile=',slsdecile, ' and sm_rate_decile=',promodecile))