在R中创建for循环

时间:2016-08-01 03:34:37

标签: r for-loop

我有四个相似的表格,但只是针对不同的细分市场。我需要使用SQL查询为每个表添加三个变量。但除了查询中的表名外,查询是相同的。是否有更聪明的方式像for循环使代码更简单?

我有四个表“合并”,“桌面”,“pWeb”,“tWeb”。我想将t1,t2和t3变量添加到这些表中,如下所示。

combine = sqldf("select region_count, visitor_count, treatment_group, local_dt, case when local_dt >= '2016-03-19' and local_dt<'2016-05-19' then 0 when local_dt>='2016-05-19' then 1 end as t2 
                from combine group by region_count, visitor_count, treatment_group, local_dt, case when local_dt >= '2016-03-19' and local_dt<'2016-05-19' then 0 when local_dt>='2016-05-19' then 1 end")
combine = sqldf("select region_count, visitor_count, treatment_group, local_dt, t2 ,case when treatment_group =  'Control' then 0 when treatment_group = 'Test' then 1 end as t3 
       from combine group by region_count, visitor_count, treatment_group, local_dt,t2,case when treatment_group =  'Control' then 0 when treatment_group = 'Test' then 1 end")
combine = sqldf("select region_count, visitor_count, treatment_group, local_dt, t2,t3 ,case when t2 =  1 and t3 =1 then 1 else 0 end as t1 
       from combine group by region_count, visitor_count, treatment_group, local_dt,t2,t3,case when t2 =  1 and t3 =1 then 1 else 0 end")

0 个答案:

没有答案