Home > database >  How to output a dataframe as text (string) mixing values and column names R
How to output a dataframe as text (string) mixing values and column names R

Time:12-02

Hi have a dataframe that is a collection of some performance metrics for ML models:

> df
# A tibble: 10 x 6
   Method                AUC    CA    F1 Precision Recall
   <chr>               <dbl> <dbl> <dbl>     <dbl>  <dbl>
 1 Logistic Regression 0.732 0.684 0.413     0.681  0.296
 2 Naive Bayes         0.729 0.694 0.463     0.679  0.352
 3 Tree                0.678 0.694 0.429     0.717  0.306
 4 Neural Network      0.674 0.684 0.413     0.681  0.296
 5 AdaBoost            0.654 0.681 0.418     0.66   0.306
 6 CN2 rule inducer    0.651 0.681 0.403     0.674  0.287
 7 kNN                 0.649 0.66  0.372     0.604  0.269
 8 SVM                 0.64  0.691 0.44      0.686  0.324
 9 SGD                 0.591 0.667 0.4       0.615  0.296
10 Constant            0.5   0.625 0         0      0   

Input:

structure(list(Method = c("Logistic Regression", "Naive Bayes", 
"Tree", "Neural Network", "AdaBoost", "CN2 rule inducer", "kNN", 
"SVM", "SGD", "Constant"), AUC = c(0.732, 0.729, 0.678, 0.674, 
0.654, 0.651, 0.649, 0.64, 0.591, 0.5), CA = c(0.684, 0.694, 
0.694, 0.684, 0.681, 0.681, 0.66, 0.691, 0.667, 0.625), F1 = c(0.413, 
0.463, 0.429, 0.413, 0.418, 0.403, 0.372, 0.44, 0.4, 0), Precision = c(0.681, 
0.679, 0.717, 0.681, 0.66, 0.674, 0.604, 0.686, 0.615, 0), Recall = c(0.296, 
0.352, 0.306, 0.296, 0.306, 0.287, 0.269, 0.324, 0.296, 0)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

I need to combine this in one row in a excel, however it is tiring copying every column name per every row. So I would like to get everything as a string (or a list of strings)saying: [Model name]: Col1_name Col1 value, Col2_name value2, ..., and so on. Something like this:

`Logistic Regression: AUC 0.732, CA 0.684, F1 0.413, Precision 0.681, Recall 0.296
 Naive Bayes: AUC 0.729, CA 0.694, F1 0.463, Precision 0.679, Recall 0.352
 Tree ... (and so on).`

It is also ok everything in one line:

Logistic Regression: AUC 0.732, CA 0.684, F1 0.413, Precision 0.681, Recall 0.296 Naive Bayes: AUC 0.729, CA 0.694, F1 0.463, Precision 0.679, Recall 0.352 Tree ... (and so on)

But I don't know how I can do this adding each column name before each value. I would appreciate any help!

CodePudding user response:

Maybe this will work for you.

Output <- apply(df, 1, function(x) {
gsub(' AUC', ': AUC', paste(paste(names(x), x), collapse = ' '))
})

Here I have assumed AUC will always be second column in the dataset. If not you may change it accordingly.

CodePudding user response:

Is this close to what you're looking for ?

my_df <- structure(list(Method = c("Logistic Regression", "Naive Bayes", 
                               "Tree", "Neural Network", "AdaBoost", "CN2 rule inducer", "kNN", 
                               "SVM", "SGD", "Constant"),
                    AUC = c(0.732, 0.729, 0.678, 0.674, 0.654, 0.651, 0.649, 0.64, 0.591, 0.5),
                    CA = c(0.684, 0.694, 0.694, 0.684, 0.681, 0.681, 0.66, 0.691, 0.667, 0.625),
                    F1 = c(0.413, 0.463, 0.429, 0.413, 0.418, 0.403, 0.372, 0.44, 0.4, 0),
                    Precision = c(0.681, 0.679, 0.717, 0.681, 0.66, 0.674, 0.604, 0.686, 0.615, 0),
                    Recall = c(0.296, 0.352, 0.306, 0.296, 0.306, 0.287, 0.269, 0.324, 0.296, 0)),
               row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"))

my_df$Solution <- paste(my_df$Method, ":",
                    colnames(my_df)[2], my_df$AUC, ",",
                    colnames(my_df)[3], my_df$CA, ",",
                    colnames(my_df)[4], my_df$F1, ",",
                    colnames(my_df)[5], my_df$Precision, ",",
                    colnames(my_df)[6], my_df$Recall, ",")
  • Related