Copy and paste data into R

How can I copy and paste data into R? For example, you see a small data set on the web, perhaps in a blog or here on StackOverflow, and you want that data set in your R session. This is a common task for many of us, and there are several ways to go about. Below I present a solution based on this blog post that I have found useful.

First, copy (e.g. “⌘ + C”) the data set.

Then, paste (e.g. “⌘ + V”) to create an R character vector:
x <- " A B C D
1: 2 2 5 3
2: 2 1 2 3
3: 3 4 4 3"

Next, use the read.table() function:
y <- read.table(text = x, header = TRUE)

Done! The data is now in a data frame:
class(y)
[1] "data.frame"

Share and Enjoy
  • Facebook
  • Twitter
  • LinkedIn
  • del.icio.us
  • StumbleUpon
  • RSS
  • email
  • PDF

Published by

Samuel

Ph.D. Student in the Department of Computer and Systems Sciences, Stockholm University.

Leave a Reply

Your email address will not be published. Required fields are marked *