简体   繁体   English

为R中不同功能的多个结果创建一个输出表

[英]Creating one output table for multiple results from different functions in R

I was trying search accordingly to find the solution for my issue but I failed, so I'm writing here. 我尝试进行相应的搜索以找到解决问题的方法,但失败了,所以在这里写。 At the moment I've created quite large set of functions in R, around 500 lines of the code for each scripts (7 scripts) producing the output of the calculation in the standardized form: 目前,我已经在R中创建了大量函数,每个脚本(7个脚本)的大约500行代码以标准化形式生成计算输出:

    >getFunction1(Key_ID)
#output
              calc1    calc2
category1       0.00     0.3
category2       0.06     0.2
category3       0.00     0.3

>getFunction2(Key_ID)
#output
              calc1    calc2
category1       0.10     0.1
category2       0.02     0.3
category3       0.01     0.3
(...)
>getFunction7(Key_ID)
#output
              calc1    calc2
category1       0.20     0.15
category2       0.04     0.4
category3       0.02     0.35

Designed functions whithin 7 scripts are same from the structure point of view but contating different calculations depend on category of the function (specific use case) and (inside the script) category of the calculations. 从结构的角度来看,这7个脚本的设计功能是相同的,但是根据功能的类别(特定用例)和(在脚本内)类别的不同,计算的不同取决于计算的类别。 Everything work perfectly but I'm not able to create one coherent table storing calculation coming from all getFunction1-7(Key_ID) in one tablelaric form looking like this: 一切工作正常,但是我无法创建一个统一的表来存储来自所有getFunction1-7(Key_ID)的一种表形式的计算,如下所示:

Key_ID|Name_Function(refering to use case)|Category(whithin the function)|Cal1|Cal2|

Key_ID is crucial part, because this is ID allowing join new calculation with the previous one in data base. Key_ID是至关重要的部分,因为这是允许与数据库中的前一个计算进行新计算结合的ID。 I cannot simply create tabelaric structure like: 我不能简单地创建表结构:

tableFunction1 <- as.data.frame(getFunction1(Key_ID)) 

cause depending on Key_ID I can receive different scores depending on the subject of the calculation, thus Key_ID referring on different objects with different attributes included into calculations. 原因取决于Key_ID我可以根据计算的主题获得不同的分数,因此Key_ID引用了计算中包含不同属性的不同对象。

Have you ever faced with kind of issue ? 您曾经遇到过类似的问题吗? Any advise so far for me ? 到目前为止对我有什么建议吗? Thanks a lot for any. 非常感谢。 Aleksandra 亚历山德拉

If the output from each function is a data frame containing categories and calculations, you need a way to add KEY_ID and the name of the function that produced the calculations. 如果每个函数的输出是包含类别和计算的数据框,则需要一种方法来添加KEY_ID和生成计算的函数的名称。 Since the OP didn't include logic of any of the functions being discussed, we'll assume that the current output is a data frame containing the columns category , calc1 , and calc2 . 由于OP不包含正在讨论的任何函数的逻辑,因此我们假设当前输出是一个包含列categorycalc1calc2的数据框。

First, since KEY_ID is passed as an argument into each of the seven functions, you can create a column in the output data frame containing this value. 首先,由于KEY_ID作为参数传递给七个函数中的每个函数,因此您可以在输出数据框中创建一个包含该值的列。

# assuming output frame has already been created, add id column
# by using rep() to repeat the value of the KEY_ID argument
output$id <- rep(KEY_ID,nrow(output))

Second, you'll want to similarly add the name of the current function as a column in your output data frame. 其次,您将需要类似地将当前函数的名称添加为输出数据框中的一列。 This can be accomplished with match.call() . 这可以通过match.call()完成。

# add name of current function to output data frame 
output$name_function <- rep(match.call()[[1]],nrow(output))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM