How to transfer a table from the clipboard to a R data.frame

Asked

Viewed 67 times

3

Motivation

In various language questionsRI see people putting the contents of a table instead of showing the steps to reproduce the data they’re analyzing. Requests to provide the result of the command dput are simply ignored.

Having to type something like

dados <- data.frame(carat = c(0.230, 0.210) ...)

at all times is tedious, subject to mistakes and a waste of time.

Question

How do I transfer the data from the table below to a data.frame using the clipboard? The famous Ctr-v

 carat cut       color clarity depth table price     x     y     z
 0.230 Ideal     E     SI2      61.5  55.0   326  3.95  3.98  2.43
 0.210 Premium   E     SI1      59.8  61.0   326  3.89  3.84  2.31
 0.230 Good      E     VS1      56.9  65.0   327  4.05  4.07  2.31
 0.290 Premium   I     VS2      62.4  58.0   334  4.20  4.23  2.63
 0.310 Good      J     SI2      63.3  58.0   335  4.34  4.35  2.75
 0.220 Fair      E     VS2      65.1  61.0   337  3.87  3.78  2.49

1 answer

4


I believe there is a platform problem to import data with ctr-v, so here are two options:

For users of Windows:

You can use the function readClipboard() for vectors. For example, enter data only from a row or column of a data frame.

For tabular data, simply use the function read.table() with clipboard as file name:

read.table(file = "clipboard", head = TRUE)

Other systems:

I recommend the function read.clipboard() package psych:

psych::read.clipboard(head = TRUE)

Take a look here for more package functions.

  • 2

    I just tested. read.table(file = "clipboard", head = TRUE) also works on linux.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.