Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

paste text with a newline/return in formatted text

Tags:

I want to do a column that is formatted to use for a mailing address and I can not get the newline/return carriage or <br/> to work when making a new column.

name = c("John Smith", "Patty Smith", "Sam Smith") address = c("111 Main St.", "222 Main St.", "555 C Street") cityState = c("Portland, OR 97212", "Portland, OR 95212", "Portland, OR 99212") df <- data.frame(name, address, cityState) 

I want to create a column that formats the data in an address label: John Smith 111 Main st. Portland, OR 97212

Each new column: will have a return after each line: so it is always 3 lines. One line for each of the other 3 columns.

# example of what I am trying to do...  paste0(name, "return", address, "return", cityState).  Everything I have tried does not work for making a newline. 
like image 848
Spruce Island Avatar asked Oct 05 '16 19:10

Spruce Island


People also ask

How do you insert a line break in paste0 R?

paste0() converts its arguments to characters and combines them into a single string without separating the arguments. We can use the <br/> tag to create a line break to have each element appear on a separate line.

What is \n line break?

For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do I print a new line in R?

Special Characters in Strings The most commonly used are "\t" for TAB, "\n" for new-line, and "\\" for a (single) backslash character.


1 Answers

To get a new line (or return) we use \n. So

addr = paste(name,  address, cityState, sep="\n") 

To view the result just use cat

> cat(addr[1]) #John Smith #111 Main St. #Portland, OR 97212 

The function cat just prints to the screen.


The other standard character is \t for a tab-space.

like image 184
csgillespie Avatar answered Sep 28 '22 00:09

csgillespie