在 R 中创建 Automatin

发布于 2024-11-17 23:15:05 字数 1261 浏览 3 评论 0原文

我创建了一个脚本,可以分析一组原始数据,并根据不同的参数和函数将其转换为许多不同的格式。我还有 152 个原始数据表要处理,但我所要做的就是在每个数据表上使用我的脚本。然而,有时我可能会决定需要更改变量或参数,并且我想在电子表格顶部提供一个参数列表,这将影响我的电子表格中的其余功能,很快就会变得非常大脚本。

  1. 全局变量并不是这个问题的答案,这个例子可以最好地说明这一点:

    超过 <- 函数(L=NULL,R=NULL)
    {
    
    if (is.null(L) | is.null(R)) 
    {
    print ("mycols: 无效的 L,R。")
    返回(空)               
    }
    选项(na.rm = TRUE)
    测试 <-(平均值(L, na.rm=TRUE)-R*sd(L,na.rm=TRUE))
    test1 <- ifelse(is.na(L), NA, ifelse(L > test, 1, 0))
    返回(测试1)
    }
    
    L=ROCC[,2]
    R=.08
    ROCC$newcolumn <- 超过(L,R)
    名称(ROCC)[名称(ROCC)=="newcolumn"]="Exceedes1"
    
    L=ROCC[,2]
    R=.16
    ROCC$newcolumn <- 超过(L,R)
    名称(ROCC)[名称(ROCC)==“新列”] =“Exceedes2”
    
    L=ROCC[,2]
    R=.24
    ROCC$newcolumn <- 超过(L,R)
    名称(ROCC)[名称(ROCC)==“新列”] =“Exceedes3”
    

所以在上面的例子中,我想在我的脚本顶部有一种方法来改变范围R 并让它影响脚本的其余部分,因为此函数将重复 152 次。我能想到的唯一方法是每次使用不同的变量复制并粘贴该函数,然后全局设置它。但我不得不想象有一个更简单的方法,我的功能可能需要重新安排?

  1. 文件名和输出名称。我不确定这是否可能,但举例来说,我所有的 input.csv 都采用一种格式,其中一个数据集的标题为 123,另一个数据集的标题为 124,另一个 125 等,然后让 R 知道获取下一个数据集,然后将该数据集输出到计算机上的特定文件夹,而无需实际输入 read.csv(file="123.csv"),然后 write.csv(example, file="123.csv) 等等?

  2. 自动化脚本的一般格式 在深入研究自动化之前,我的程序是复制并粘贴脚本 152 次,然后更改每个脚本的文件名和输出名称。这听起来很荒谬,但由于我缺乏编程技能,我不确定有更好的方法来改变它。有什么想法吗?

感谢您提前提供的所有帮助。

I have created a script that analyzes a set of raw data and converts it into many different formats based on different parameters and functions. I have 152 more raw data sheets to go, but all I will have to do is use my script on each one. However, there will be times that I might decide I need to change a variable or parameter and I would like to come up with a parameter list at the top of my spreadsheet that would affect the rest of the functions in my soon to be very large script.

  1. Global variables aren't the answer to this problem, this is best illustrated through this example:

    exceedes <- function (L=NULL, R=NULL)
    {
    
    if (is.null(L) | is.null(R)) 
    {
    print ("mycols: invalid L,R.")
    return (NULL)               
    }
    options (na.rm = TRUE)
    test <-(mean(L, na.rm=TRUE)-R*sd(L,na.rm=TRUE))
    test1 <- ifelse(is.na(L), NA, ifelse(L > test, 1, 0))
    return (test1)
    }
    
    L=ROCC[,2]
    R=.08
    ROCC$newcolumn <- exceedes(L,R)
    names(ROCC)[names(ROCC)=="newcolumn"]="Exceedes1"
    
    L=ROCC[,2]
    R=.16
    ROCC$newcolumn <- exceedes(L,R)
    names(ROCC)[names(ROCC)=="newcolumn"]="Exceedes2"
    
    L=ROCC[,2]
    R=.24
    ROCC$newcolumn <- exceedes(L,R)
    names(ROCC)[names(ROCC)=="newcolumn"]="Exceedes3"
    

So in the above example, I would like to have a way at the top of my script to change the range of R and have it affect the rest of the script because this function will be repeated 152 times. The only way I can think of doing it is to copy and paste the function over and over with a different variable each time, and set it globally. But I have to imagine there is a simpler way, my function possibly needs to be rearranged perhaps?

  1. File names and output names. I am not sure whether this is possible but say for example that all my input.csv's come in a format where one dataset will be titled 123 another will be 124, another 125 etc. and then have R know to take the very next dataset, and then output that dataset to a specific folder on my computer without me having to actually type in read.csv(file="123.csv"), and then write.csv(example, file="123.csv) and so on?

  2. General formatting of automation script
    Before I dive into my automation, my procedure was going to be to copy and past the script 152 times over and then change the filename and output name for each one. This sounds ridiculous, but with my lack of programming skills I am not sure a better way to change it. Any ideas?

Thanks for all the help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开柳相依 2024-11-24 23:15:05

您可以通过构造参数向量(例如 R)来使用不同参数重新运行该函数

R <- c(seq(0.1, 1, by = 0.01))

,然后运行 ​​exceedes 函数 length(R) 次使用sapply

exceedes <- function(R, L) {} #notice the argument order
sapply(X = R, FUN = exceedes, L = ROCC[, 2])

您可以将其他参数传递给您的函数(例如file.name)并使用它来创建您需要的任何文件名。

You can rerun the function with different parameters by constructing a vector of paremters (say R)

R <- c(seq(0.1, 1, by = 0.01))

and then run your exceedes function length(R) times using sapply.

exceedes <- function(R, L) {} #notice the argument order
sapply(X = R, FUN = exceedes, L = ROCC[, 2])

You can pass other arguments to your function (e.g. file.name) and use it to create whatever file name you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文