Search code examples
rexport-to-excelline-breakspaste

Paste two columns separated by a linebreak command that Excel understands


How to paste two columns of a dataframe together so that MS Excel will insert a line break between them, when pasted into Excel.

The result of the code below should look something like this picture when pasted into MS Excel. Notice that the last column has a line break between "Qn1" and "Quebec":

enter image description here

dfExample <- head( CO2 )  ## CO2 is in base datasets

dfExample$Vektor.With.Linebreak <- paste(   df$Plant, df$Type, sep = "(  linebreak here  )" )


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

Solution

  • The newline character sequence for Windows is \r\n, so use:

    dfExample$Vektor.With.Linebreak <- paste(dfExample$Plant, dfExample$Type, sep = "\r\n")