How to write strings with new lines in R?


While writing the string vectors, we get them in a single line but we might want to represent strings in different lines especially in cases where each of the value of the string vector has a different meaning. This is helpful to the programmer as well as to any other reader. We can change the single line to multiple new lines using writeLines function in R.

Example

Reading with single line −

> String1<-c("Covid-19","2020","Lockdown","Quarantine","Life Changing")
> String1
[1] "Covid-19" "2020" "Lockdown" "Quarantine" "Life Changing"

Reading the same vector with new lines −

> String1<-writeLines(c("Covid-19","2020","Lockdown","Quarantine","Life Changing"))
Covid-19
2020
Lockdown
Quarantine
Life Changing
> String2<-c("Tutorialspoint","SIMPLY EASY LEARNING","You are browsing the best resource for Online Education","Library","Videos","Ebooks","GATE Exams",
+ "Coding Ground","Current Affairs","UPSC Notes","Whiteboard","Net Meeting","Tutorix")
> writeLines(String2)
Tutorialspoint
SIMPLY EASY LEARNING
You are browsing the best resource for Online Education
Library
Videos
Ebooks
GATE Exams
Coding Ground
Current Affairs
UPSC Notes
Whiteboard
Net Meeting
Tutorix
> String3<-c("Office","IT","Academic","Development","Business","Design","Others")
> writeLines(String3)
Office
IT
Academic
Development
Business
Design
Others
> String4<-c("Machine Learning","Computer Science","Web Development","Mobile App Development","Database Management","Microsoft Technologies",
+ "Big Data and Analytics")
> writeLines(String4)
Machine Learning
Computer Science
Web Development
Mobile App Development
Database Management
Microsoft Technologies
Big Data and Analytics

Updated on: 11-Aug-2020

249 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements