Vectors are a common data structure in R. We use vectors to store a sequence of values of the same type. In this tutorial, we will look at how to print a vector in R with the help of some examples.
How to print a vector in R?
You can use the R built-in print()
function to print the contents of a vector in R. Pass the vector you want to print as an argument to the function. The following is the syntax –
# display vector print(vec)
It prints the vector in a single line with elements separated by a space.
Examples
Let’s look at some examples of displaying a vector in R. First, we will create a vector that we will be using throughout this tutorial.
# create a vector vec <- c(10, 20, 30, 40, 50)
We now have a vector containing some numbers.
Print an R vector
Let’s use the above function to print the vector, vec
created above.
# display the vector print(vec)
Output:
[1] 10 20 30 40 50
We get the contents of the vector as the output.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Print vector as a string
Let’s now print the above vector as a string. You can use the paste()
function in R to print the contents of a vector as a single string. You can also specify a separator to use between the vector values with the collapse
parameter of the paste()
function.
For example, let’s convert the above vector to a string where the values are separated by a single space.
# display vector as string paste(vec, collapse = " ")
Output:
'10 20 30 40 50'
You can see that we get a string output with the vector values.
Print vector with values separated by comma
Let’s now use the paste()
function demonstrated above to print the values as a string separated by a comma.
# display vector as string with comma as separator paste(vec, collapse = ",")
Output:
'10,20,30,40,50'
You can see that the vector values in the output are comma-separated.
Alternatively, you can also use the cat()
function in R to display values in a vector as comma-separated values.
# display vector as comma separated values cat(vec, sep=",")
Output:
10,20,30,40,50
You can see that we get the desired result. Unlike the output from the paste()
function, the output from the cat()
function doesn’t have any quotes.
Print vector in multiple lines
You can use the cat()
function with '\n'
as the value for the sep
parameter to display vector values on separate lines in the output.
# display vector values line by line cat(vec, sep = "\n")
Output:
10 20 30 40 50
You can see that the vector values are displayed in multiple lines.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.