Suppose we have the following data frame called data
, produced by the code immediately beneath:
> data
ID Period Values
1 1 1 5
2 1 2 10
3 1 3 15
4 2 1 12
5 2 2 0
6 2 3 2
7 3 1 4
8 3 2 -8
9 3 3 3
data <-
data.frame(
ID = (c(1,1,1,2,2,2,3,3,3)),
Period = as.numeric(c(1, 2, 3, 1, 2, 3, 1, 2, 3)),
Values = as.numeric(c(5, 10, 15, 12, 0, 2, 4, -8, 3))
)
Next, we use the below simple code in base R to copy and paste data
into an Excel sheet:
write.table(x = data,
file = "clipboard",
sep = "\t",
row.names = FALSE,
col.names = TRUE
)
When copy/pasting data into Excel, I'd like a row of text describing the table inserted immediately above the table (such as: Table name is "Data"), as shown in the image below.
How can the above code be modified to insert a text row above the table? In base R preferably?