In this tutorial, we will look at how to create a vector of ones (having 1 as its elements) in R with the help of some examples.
How to create a vector of ones in R?
You can create a vector of ones in R using different methods. For example, using the c()
function, using the rep()
function, etc. Let’s look at these methods with the help of examples.
Using c()
function
The c()
is used to create a vector by combining different values (and/or vectors) together. You can use the c()
function to create a vector of ones if the length of the vector is relatively small.
For example, let’s create a vector of ones of length four.
# create a vector of ones vec <- c(1, 1, 1, 1) # display the vector print(vec)
Output:
[1] 1 1 1 1
We get a vector of four ones.
Manually typing 1
can become cumbersome if you want to create a large vector of ones. Thus, it’s recommended to use the rep()
function.
Using rep()
function
You can use the rep()
function in R to create a vector with a value repeated a specified number of times. The following is the syntax –
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.
rep(x, t)
It creates a vector with the value x repeated t times.
To create a vector of ones, pass 1 as the first argument to the rep()
function and the length of the vector as its second argument.
Let’s look at an example. We’ll create the same vector as above – a vector of four ones.
# create a vector of ones vec <- rep(1, 4) # display the vector print(vec)
Output:
[1] 1 1 1 1
We get a vector of four ones. The same result we got above.
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.