Copy an R data.frame to an Excel spreadsheet

后端 未结 3 1517
一个人的身影
一个人的身影 2021-02-02 08:39

I have a question which is exactly similar to this question.

As part of my work, I have to copy output from the R Studio Console to an excel worksheet in order to make e

3条回答
  •  时光取名叫无心
    2021-02-02 09:15

    I usually source the following function:

    cb <- function(df, sep="\t", dec=",", max.size=(200*1000)){
        # Copy a data.frame to clipboard
        write.table(df, paste0("clipboard-", formatC(max.size, format="f", digits=0)), sep=sep, row.names=FALSE, dec=dec)
      }
    

    A few notes:

    • Max.size allows you to specify how big the clipboard can become (in kilobytes) before it cancels, it's set to ~200MB right now.
    • It works perfectly for copying an R dataframe from an R studio session to Excel (with my EU locale). You might have to adjust the separator / decimal symbols to make it work with US versions.

    How to use:

    df <- mtcars
    cb(df)
    # Paste in excel as 'values'
    

提交回复
热议问题