Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Repeat Vectors in R

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I repeat vectors in R?
 
Sheriff
Posts: 3034
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the rep function:

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You  can use the rep() function in several ways if you want to repeat the complete vector.
For examples: specify the argument times 1. To repeat the vector c(0, 0, 7) three times,
use this code:

> rep(c(0, 0, 7), times = 4)
[1] 0 0 7 0 0 7 0 0 7 0 0 7 2

We can also repeat every value by specifying the argument each, like this:

> rep(c(2, 4, 2), each = 2)
[1] 2 2 4 4 2 2 3

We can tell R for each value how often it has to repeat:

> rep(c(0, 7), times = c(4,3))
[1] 0 0 0 0 7 7 7 4

In seq, we use the argument length.out to define R. it will repeat the vector until it reaches that length,
even if the last repetition is incomplete.

> rep(1:3,length.out=9)
[1] 1 2 3 1 2 3 1 2 3
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!