Search code examples
rreproducible-researchreprex

how do I reprex reproduce a data frame in R?


I sometimes have to copy data from Excel into R. The workflow goes something like this:

# Step 1: Highlight Excel spreadsheet to be copied into R
# Step 2: Run this command to get the data into R
excelss <- read.delim("clipboard")  # for Windows

If I print(excelss) I get my data frame

  Excel.Col.1  Excel.Col.2
1           A           24
2           B            5
3           C           53

The question is: How do I take this data frame output, and permanently save it in my script? What reprex commands do I use? So that the next time I open the script the data frame will be right there, and I don't have to open Excel and go through the whole copy/paste routine again?

Or another way to put it. How do I take console data frame output and save it to my editor?


Solution

  • Use read.table(header = TRUE, sep = "\t", quote = "\"", dec = ".", fill = TRUE, comment.char = "", text="...") i.e. other parameters beside text= are set as in read.delim() Usually I use read.table(header=TRUE, text="..."), e.g. for your data:

    excelss <- read.table(header=TRUE, text=
    "      Excel.Col.1  Excel.Col.2
                   A           24
                   B            5
                   C           53")
    

    or

    excelss <- read.table(header=TRUE, text=
    "  Excel.Col.1  Excel.Col.2
    1           A           24
    2           B            5
    3           C           53")
    excelss