Questions tagged [rep]

rep is a base package function in R to replicate the elements of a vector

rep is a base function in the R programming language . It takes a vector or other data structure and replicates its elements the specified number of times.

179 questions
23
votes
4 answers

Generate series 1, 2,1, 3,2,1, 4,3,2,1, 5,4,3,2,1

I am trying to generate a vector containing decreasing sequences of increasing length, such as 1, 2,1, 3,2,1, 4,3,2,1, 5,4,3,2,1, i.e. c(1, 2:1, 3:1, 4:1, 5:1) I tried to use a loop for this, but I don't know how to stack or concatenate the…
FFB
  • 255
  • 2
  • 7
21
votes
4 answers

Create a sequence of sequences of numbers

I would like to make the following sequence in R, by using rep or any other function. c(1, 2, 3, 4, 5, 2, 3, 4, 5, 3, 4, 5, 4, 5, 5) Basically, c(1:5, 2:5, 3:5, 4:5, 5:5).
Rene
  • 363
  • 2
  • 7
16
votes
6 answers

Replicating a dataframe as a whole n times

I am trying to replicate a dataframe (zoo object) 50 times as a whole, and get the result as a matrix, but all the commands I have tried seems to be unsuccessful. I could easily write a function that would do this, but I was hoping the result could…
Mayou
  • 8,498
  • 16
  • 59
  • 98
15
votes
4 answers

calculate median from data.table columns in R

I am trying to calculate a median value across a number of columns, however my data is a bit funky. It looks like the following example. library(data.table) dt <- data.table("ID" = c(1,2,3,4),"none" = c(0,5,5,3), "ten" =…
Dan
  • 2,625
  • 5
  • 27
  • 42
13
votes
1 answer

How to create a matrix from vector returned by rep() function?

x=1:20 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 rep(x,2) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 View(rep(x,2)) Having a problem with…
gabriel
  • 399
  • 2
  • 7
  • 17
11
votes
1 answer

Create list of vectors using rep()

I want to create a list which is eight times the vector c(2,6), i.e. a list of 8 vectors. WRONG: object = as.list(rep(c(2,6),8)) results instead in a list of 16 single numbers: 2 6 2 6 2 6 2 6 ... I tried drop=0 but that didn't help, and I can't…
dez93_2000
  • 1,730
  • 2
  • 23
  • 34
10
votes
3 answers

Calling print(ls.str()) in function affect behavior of rep

Begin a new R session with an empty environment. Write a series of functions with a parameter that is to be used as the value of the times parameter in a call to rep(). f <- function(n) { rep("hello", times = n) } f(x) One expect this to fail,…
Homer White
  • 733
  • 1
  • 5
  • 14
10
votes
1 answer

More than one value for "each" argument in "rep" function?

How to assign more than one value for "each" argument in "rep" function in R? A trivial example, where each value in a vector is 3-times repeated in a row: a <- seq(2,6,2) rep (a,each = 3) However, if I add more than one value in "each" argument…
user36478
  • 346
  • 6
  • 14
8
votes
4 answers

Sequence of two numbers with decreasing occurrence of one of them

I would like to create a sequence from two numbers, such that the occurrence of one of the numbers decreases (from n_1 to 1) while for the other number the occurrences are fixed at n_2. I've been looking around for and tried using seq and rep to do…
Seb
  • 370
  • 2
  • 14
6
votes
2 answers

rep function in R

When I perform: rep(1:4, rep(4,4)) I get 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 which is expected. But then when I try to fix the length to 16(which is the length of the output) as follows: rep(1:4, rep(4,4), length.out = 16) I get 1 2 3 4 1 2 3 4 1 2…
Vinayak Agarwal
  • 1,350
  • 3
  • 15
  • 28
5
votes
3 answers

Insert rows in a dataframe based on a calculation

I am pretty new to R and I am trying to copy a calculation done in Excel to R. I have a data frame like this: Component <- c("A", "B", "C") Report_Time <- c(5781, 5781, 5781) Interval <- c(700, 600, 800) End_Time <- c(8281, 8281, 8281) Start_Time…
Madhu
  • 97
  • 6
4
votes
4 answers

how to create a vector of blank spaces of different sizes in R

I have a vector of "sizes" or "widths", like this one: x <- c(1, 4, 6, 10) and I would like to create another vector based on x, a vector like this one: y <- c(" ", " ", " ", " ") Basically there are two imputs for creating y: the…
Miquel
  • 442
  • 3
  • 14
4
votes
2 answers

Repeat pattern in R (integers down and up)

I would like to create a repeat pattern from 5 to 0 then goes back to 5, 3 times, so I want 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, ..., 5 How can I do this is R?? I know rep (seq(from=a, by=b, length=c),d) function, but don't know how to use it with this…
Cindy
  • 425
  • 1
  • 3
  • 10
4
votes
5 answers

How can I create this special sequence?

I would like to create the following vector sequence. 0 1 0 0 2 0 0 0 3 0 0 0 0 4 My thought was to create 0 first with rep() but not sure how to add the 1:4.
ZWL
  • 319
  • 2
  • 4
4
votes
1 answer

R rep function with for loop

I have a year range 1981:1984. How can use rep() function to get the following results: For each round rep, the latest year is dropped off: first get 1981:1984, then 1981:1983, then 1981:1982, then 1981 as shown in the…
dennis
  • 119
  • 1
  • 8
1
2 3
11 12