简体   繁体   中英

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:

    >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. 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:

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. 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.

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. 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 .

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.

# 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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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