【问题标题】:run the script according to logical conditions in R根据 R 中的逻辑条件运行脚本
【发布时间】: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


    【解决方案1】:

    考虑by 内部的if 逻辑,即按因子对数据帧进行切片的方法。并通过命令行使用system() 调用Rscript 运行其他脚本(假设R bin 目录设置为您的PATH 环境变量):

    by_list <- by(df, df[,c("SKU", "acnumber", "year")], function(sub) {
    
      if (sum(sub$action == 1) %in% c(1:4))   system("Rscript /path/to/script1.r")
      if (sum(sub$action == 1) >= 5)          system("Rscript /path/to/script2.r")
    
      return(sub)
    })
    

    更好的是,source() 主脚本中的外部脚本,确保将两个脚本的整个过程包装在 function() 调用中,甚至添加特定 SKU 之类的参数。否则,source 运行这些文件。使用这种方法,您可以返回输出。

    source("/path/to/script1.r")   # IMPORTS script1_function()
    source("/path/to/script2.r")   # IMPORTS script2_function()
    
    by_list <- by(df, df[,c("SKU", "acnumber", "year")], function(sub) {
    
      current_SKU <- max(sub$SKU)   # OR min(sub$SKU) OR sub$SKU[[1]]
    
      if (sum(sub$action == 1) %in% c(1:4))  output <- script1_function()
      if (sum(sub$action == 1) >= 5)         output <- script2_function()
    
      return(output)
    })
    

    【讨论】:

    • 感谢你。 Parfait,我理解你的意思吗,我必须在这里写我的组 by_list
    猜你喜欢
    • 2020-04-17
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2012-05-17
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多