R outputs to dataframe

Is there any package available to convert R outputs to dataset or dataframe so that it can be exported to SAS?

Did you check the function write.foreign() in the foreign package? It should already be installed in the default R installation.

1 Like

As far i know write.foreign() works for only outputs that are in data frame structure but many of the R outputs don't follow data frame "cannot coerce class to a data.frame" issue comes up!

What kind of data are you thinking about?

Any chance you can post a reprex of the code & ouput? Is it a crosstab, regression result, something else? But even without seeing specifics, seems like you'd want to convert the output to a df, then write to a text or SAS file.

Thanks Alexis the output looks like this,trying to convert this to data frame

10000 bootstrap samples
95% confidence intervals
Comparing vac to con

Mitigated Fraction

            observed median  lower  upper

Equal Tailed 0.44 0.4432 0.1424 0.7056
Highest Density 0.44 0.4432 0.1488 0.7088

Hodges-Lehmann

            observed  median      lower        upper

Equal Tailed -0.07335 -0.0692 -0.1704581 -0.015220000
Highest Density -0.07335 -0.0692 -0.1549000 -0.007775765

Quartile Differences (quartiles of vac - quartiles of con)

 observed    median    lower     upper

Q25 -0.041500 -0.041300 -0.10340 -0.000905
Q50 -0.112525 -0.111175 -0.28115 0.023200
Q75 -0.168000 -0.168000 -0.38890 0.023975

Quartiles of con
observed median lower upper
Q25 0.054000 0.054000 0.021005 0.11275
Q50 0.139275 0.139275 0.061400 0.31000
Q75 0.315000 0.315000 0.173000 0.45250

Quartiles of vac
observed median lower upper
Q25 0.01250 0.01250 0.00125 0.026000
Q50 0.02675 0.02675 0.01665 0.144575
Q75 0.14700 0.14700 0.02810 0.219250

#--------R libraries---------
library(tidyverse)
library(MF)
MFSubj(lesion ~ group, calflung)

boot<-HLBoot(lesion ~ group, calflung, compare = c("con", "vac"), b = 100,
B = 100, alpha = 0.05, hpd = TRUE, bca = FALSE,
return.boot = FALSE, trace.it = FALSE, seed = NULL)

Trying to convert the above output to dataframe

I don't know about packages that export all that information at once, they might exist. But indeed, the boot object is complex and contains many information that can not be saved as a single data.frame. But it's possible to extract the parts that are of interest as data.frames and save them.

Here are the named data fields in the boot object, they can be accessed with $:

names(boot)
# [1] ".refClassDef" "compare"      "seed"         "QDIFstat"    
# [5] "rng"          ".->HLstat"    ".->sample"    "alpha"       
# [9] "QXstat"       ".->QYstat"    ".->MFstat"    ".->QDIFstat" 
# [13] ".->nboot"     ".->seed"      "field"        ".->compare"  
# [17] "HLstat"       "nboot"        ".->QXstat"    ".self"       
# [21] ".->alpha"     ".->rng"       "QYstat"       "sample"      
# [25] "MFstat"       "show"     

The ones that are printed by default are:

# Mitigated Fraction
boot$MFstat
# observed median  lower  upper
# Equal Tailed        0.44 0.4464 0.1424 0.7056
# Highest Density     0.44 0.4464 0.1520 0.7120

# Hodges-Lehmann

boot$HLstat
# observed   median    lower     upper
# Equal Tailed    -0.07335 -0.07335 -0.17175 -0.015505
# Highest Density -0.07335 -0.07335 -0.16050 -0.009750

# Quartile Differences
boot$QDIFstat
# observed    median    lower     upper
# Q25 -0.041500 -0.041500 -0.10340 -0.000905
# Q50 -0.112525 -0.111175 -0.28190  0.023200
# Q75 -0.168000 -0.168000 -0.38656  0.005300

# Quartiles of con
boot$QXstat
# observed   median    lower   upper
# Q25 0.054000 0.054000 0.021005 0.11275
# Q50 0.139275 0.139275 0.061400 0.31000
# Q75 0.315000 0.315000 0.173000 0.45250

# Quartiles of vac
boot$QYstat
# observed  median   lower    upper
# Q25  0.01250 0.01250 0.00095 0.026000
# Q50  0.02675 0.02675 0.01665 0.144575
# Q75  0.14700 0.14700 0.02810 0.219250

Here, since they all have the same number and names of columns, it is possible to assemble them in a single data.frame (but obviously that wouldn't work with unmatched columns). You also need to add a column to know what statistic each row describes (since once they're assembled it would be harder to know).

library(tidyverse)

bind_rows(
  as_tibble(boot$MFstat) %>%
    add_column(stat = "Mitigated Fraction"),
  as_tibble(boot$HLstat) %>%
    add_column(stat = "Hodges-Lehmann"),
  as_tibble(boot$QDIFstat) %>%
    add_column(stat = "Quartile Differences"),
  as_tibble(boot$QXstat) %>%
    add_column(stat = "Quartiles of X"),
  as_tibble(boot$QYstat) %>%
    add_column(stat = "Quartiles of Y"))
# A tibble: 13 x 5
#   observed  median    lower     upper stat                
#      <dbl>   <dbl>    <dbl>     <dbl> <chr>               
# 1   0.44    0.446   0.142    0.706    Mitigated Fraction  
# 2   0.44    0.446   0.152    0.712    Mitigated Fraction  
# 3  -0.0734 -0.0734 -0.172   -0.0155   Hodges-Lehmann      
# 4  -0.0734 -0.0734 -0.161   -0.00975  Hodges-Lehmann      
# 5  -0.0415 -0.0415 -0.103   -0.000905 Quartile Differences
# 6  -0.113  -0.111  -0.282    0.0232   Quartile Differences
# 7  -0.168  -0.168  -0.387    0.00530  Quartile Differences
# 8   0.054   0.054   0.0210   0.113    Quartiles of X      
# 9   0.139   0.139   0.0614   0.31     Quartiles of X      
# 10   0.315   0.315   0.173    0.452    Quartiles of X      
# 11   0.0125  0.0125  0.00095  0.026    Quartiles of Y      
# 12   0.0268  0.0268  0.0167   0.145    Quartiles of Y      
# 13   0.147   0.147   0.0281   0.219    Quartiles of Y   

Thank you very much Alexis,this workaround definitely helps :+1: :slightly_smiling_face:

This topic was automatically closed after 45 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.