【发布时间】:2018-06-24 12:27:00
【问题描述】:
在我的数据集中,我使用组(层)SKU-acnumber-year。 这里的小例子:
df=structure(list(SKU = c(11202L, 11202L, 11202L, 11202L, 11202L,
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L,
11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L, 11202L
), stuff = c(8.85947691, 9.450108704, 10.0407405, 10.0407405,
10.63137229, 11.22200409, 11.22200409, 11.81263588, 12.40326767,
12.40326767, 12.40326767, 12.99389947, 13.58453126, 14.17516306,
14.76579485, 15.94705844, 17.12832203, 17.71895382, 21.26274458,
25.98779894, 63.19760196), action = c(0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L),
acnumber = c(137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L,
137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L,
137L, 137L, 137L), year = c(2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L,
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L)), .Names = c("SKU",
"stuff", "action", "acnumber", "year"), class = "data.frame", row.names = c(NA,
-21L))
非常重要:
action 列只有两个值 0 和 1。正如我们在这个例子中看到的,有 1 个动作类别的东西有 3 个观察值,0 类别的东西有 18 个 obs。
我需要设置逻辑条件。 因此,对于具有 1 到 4 个观察值的 1 类动作的组,然后运行 script1.r
对于通过 1 类行动具有 >=5 观察值的组,则必须运行 script2.r
我会这样想象,script3.r 被创建, 具有以下内容(条件),但我不知道如何正确设置这些逻辑条件。
# i take data from sql
dbHandle <- odbcDriverConnect("driver={SQL Server};server=;database=;trusted_connection=true")
sql <- paste0(select needed columns)
df <- sqlQuery(dbHandle, sql)
for groups where from 1-4 observations by stuff of 1 category of action then C:/path to/скрипт1.r
(or if groups have from 1-4 observations by stuff of 1 category of action then C:/path to/script1.r)
for groups where >=5 observations by stuff of 1 category of action then C:/path to/script2.r
( of if groups have >=5 observations by stuff of 1 category of action then C:/path to/script2.r)
我该如何实现呢? script.3r 按计划运行,它将按照计划运行,以便运行两个脚本。 我只是不想单独为每个脚本制作我的 Shedule。
【问题讨论】:
标签: r if-statement odbc rodbc