Print, cat, paste in R separated by newline character

17k Views Asked by At

I want to do the print the elements of a vector line by line in R like below

1

2

3

However, when I do paste(c(1,2,3), "\n") or paste(c(1,2,3),sep = "\n"), the new line never gets printed out. The same thing goes for cat as well. I always get the following:

"1" "2" "3"

I would like to know to get around this problem.

3

There are 3 best solutions below

1
On

Try:

cat(paste(1:3), sep="\n")
1
2
3
0
On

writeLines is designed for this purpose. You need to supply a character vector though:

writeLines(as.character(1:3))
1
2
3
0
On

The followings work in RStudio:

cat(1,2,3,sep="\n")

and

cat(1:3,sep="\n")

Both outputs are

1
2
3