R-Tip: Copy and paste data from/to Excel

Whilst there are a suite of libraries for importing/exporting data to/from R (the newest arrival is readit which makes it really easy to read most rectangular data) there are occasions when a quick copy and paste from/to Excel is useful.

 

Andrew Landgraf’s solution is based on two functions:-

# Copy from Excel to R

read.excel <- function (header=TRUE,...) {

read.table ("clipboard",sep="\t",header=header,...)

}

 

# Paste from R to Excel

write.excel <- function (x,row.names=FALSE,col.names=TRUE,...) {

write.table (x,"clipboard",sep="\t",row.names=row.names,col.names=col.names,...)

}

 

Just copy and paste the two functions functions into R.

 

To paste data from Excel to R, copy (ctrl-c) the cells from Excel and then run the following in R

df <- read.excel ()

 

# to write a data set to Excel, simply run this command in R and then paste (ctrl-p) in Excel

write.excel (df)