These notes explain how to move data between R and Excel and other Windows applications via the clipboard.

writeClipboard

R has a function writeClipboard that does what the name implies. Notwithstanding, the statement to writeClipboard may need to exist cast to a character blazon. For example the code

> x <- "hello globe" > writeClipboard(x)            

copies the cord "hi world" to the clipboard as expected. However the code

> x <- three.xiv > writeClipboard(x)            

produces the error message

Fault in writeClipboard(str, format) :    argument must be a character vector or a raw vector            

The solution is to call writeClipboard( as.character(ten) ), casting the object x to a grapheme string.

All variables in R are vectors, and elements of a vector can have differing types. If ane chemical element of a vector is a character string, all elements will exist bandage to strings without the need for an explicit equally.character statement. Later a vector has been copied to the clipboard, the elements of the vector volition be separated past newlines when pasted into a document.

readClipboard

The companion part for writeClipboard is readClipboard.

The command

10 <- readClipboard()

will assign the contents of the clipboard to the vector x. Each line becomes an element of x. The elements will be graphic symbol strings, even if the clipboard contained a column of numbers before the readClipboard command was executed. If y'all select a cake of numbers from Excel, each row becomes a single cord containing tabs where there were originally prison cell boundaries.

scan

You can utilize the scan function to re-create a column of numbers from Excel to R. Copy the column from Excel, run x <- scan(), type Ctrl-v to paste into R, and press enter to signal the terminate of input to scan. Then x will contain the numbers from Excel as numbers, non as quoted strings. Note that browse only works with columns of numbers. R volition produce an fault message if the copied cavalcade contained a string. If in that location is an empty cell, only the numbers in a higher place the first empty prison cell will be copied into the R vector.

Note that scan works with columns in Excel. If you copy a row of numbers from Excel and phone call browse, the numbers will be concatenated into a single number in R. For case, if you lot copy horizontally adjacent cells containing xix and 44 and run x <- scan(), and so 10 will contain 1944. To re-create a row from Excel, first transpose the row in Excel, then copy the consequence as a column.

The part browse() is non limited to Excel. Information technology could be used to paste a column of numbers copied from other applications, such equally Word or Notepad.

read.tabular array and write.table

The functions above only work with columns of data; rows are combined into single entries. To move a block of cells from

The lawmaking write.table(x, "clipboard", sep="\t") will re-create a table x to the clipboard in such a style that it can be pasted into Excel preserving the table structure. By default, the row and cavalcade names will come up forth with the table contents. To leave the row names behind, add the argument row.names=False to the call to write.table.

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

Similarly, add col.names=Simulated if you do not desire the row names to come over to Excel.

write.table(ten, "clipboard", sep="\t", row.names=Imitation, col.names=FALSE)            

Click to learn more about Bayesian statistics consulting

See likewise: R for programmers