1

I got a report from a large function in R. It prints a table for plotting. I copied the text format output like this

cyl        wt       mpg        se      LLCI      ULCI
     4.0000    2.1568   27.3998    0.7199   25.9250   28.8746
     6.0000    2.1568   23.2805    0.8261   21.5882   24.9727
     8.0000    2.1568   19.1611    1.5544   15.9770   22.3452
     4.0000    3.3250   21.0658    1.2733   18.4575   23.6742
     6.0000    3.3250   18.8352    0.6544   17.4947   20.1758
     8.0000    3.3250   16.6046    0.7792   15.0084   18.2008
     4.0000    3.8436   18.2540    1.8031   14.5604   21.9476
     6.0000    3.8436   16.8619    0.8921   15.0345   18.6892
     8.0000    3.8436   15.4697    0.6120   14.2161   16.7234

and pasted it using x <- readClipboard(). Then I summary(x) and got

Length     Class      Mode 
       10 character character

How can I change x into a numeric table with headings for plotting? Thanks!

MC2020
  • 67
  • 6
  • Maybe wt as x and mpg as y. Just some lines. The problem is that I don't know how to convert the text format table to a data frame. I can do it in excel and get it back to R, but it would be awesome to know how to do that in R itself. – MC2020 Dec 29 '20 at 16:46
  • can you please try the solution below – akrun Dec 29 '20 at 16:47

1 Answers1

0

If we need to convert to a data.frame, an option is soread after copying the text (Ctrl + C)

library(overflow)
df1 <- soread()

The output is a data.frame which can be used for plotting

str(df1)
#'data.frame':  9 obs. of  6 variables:
# $ cyl : num  4 6 8 4 6 8 4 6 8
# $ wt  : num  2.16 2.16 2.16 3.33 3.33 ...
# $ mpg : num  27.4 23.3 19.2 21.1 18.8 ...
# $ se  : num  0.72 0.826 1.554 1.273 0.654 ...
# $ LLCI: num  25.9 21.6 16 18.5 17.5 ...
# $ ULCI: num  28.9 25 22.3 23.7 20.2 ...

Once, we have the data.frame, it can be used with ggplot after reshaping into 'long' format

library(ggplot2)
library(dplyr)    
library(tidyr)
df1 %>%
   mutate(rn = row_number()) %>% 
   pivot_longer(cols = -rn) %>% 
   ggplot(aes(x = rn, y = value, color = name)) +
      geom_line() +
      theme_bw()

enter image description here

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks, akrun! But my rstudio told me that package ‘overflow’ is not available (for R version 4.0.2). Is there anything else I can do? Thanks! – MC2020 Dec 29 '20 at 16:50
  • @MC2020 you can install from [github](https://github.com/mrdwab/overflow-mrdwab) sorry should have mentioned earlier – akrun Dec 29 '20 at 16:50
  • Yeah, I just googled it and found the solution. Very nice of you for plotting that! Your answer really helps! Thank you so much. – MC2020 Dec 29 '20 at 16:53