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
4
votes
2 answers

How to create particular month and/or date sequence "n" number of times

I am trying to create a sequence of months from Jan- Dec 3 times. ie. Jan Feb Mar . . . . Dec Jan Feb Mar . . . . Dec Jan Feb Mar . . . . Dec I tried this…
Harshad M
  • 41
  • 6
4
votes
2 answers

Repeat vector when its length is not a multiple of desired total length

I have a data frame with 1666 rows. I would like to add a column with a repeating sequence of 1:5 to use with cut() to do cross validation. It would look like this: Y x1 x2 Id1 1 .15 3.6 1 0 1.1 …
screechOwl
  • 27,310
  • 61
  • 158
  • 267
3
votes
4 answers

Repeat sequence every nth element x times

I have a vector e.g: v <- c(1, 2, 3, 4) I want to repeat the sequence of every nth element x times e.g: x=2 n= 2 [1] 1, 2, 1, 2, 3, 4, 3, 4 I know that rep(v, times=n) [1] 1, 2, 3, 4, 1, 2, 3, 4 rep(v, each=n) [1] 1, 1, 2, 2, 3, 3, 4, 4 Thanks!
3
votes
3 answers

Is there a simpler version of renaming columns with alternating patterns? Or tidyverse methods?

My Data So I have a data frame that I am working with below: structure(list(V1 = c(3L, 3L, 3L, 2L, 4L, 1L), V2 = c(1L, 1L, 1L, 1L, 1L, 1L), V3 = c(2L, 2L, 2L, 1L, 3L, 2L), V4 = c(2L, 2L, 3L, 1L, 1L, 1L), V5 = c(3L, 3L, 4L, 1L, 3L, 3L), V6 = c(3L,…
Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
3
votes
4 answers

Get (1, 2, 3, 4, 2, 3, 4, 3, 4, 4) from (1, 2, 3, 4) in R

I have vector z1 <- c(1, 2, 3, 4) and want to get from it another vector z2 <- c(1, 2, 3, 4, 2, 3, 4, 3, 4, 4) I've tried different variants of rep() function, but get only this z3 <- rep(z1,…
Slava
  • 65
  • 4
3
votes
2 answers

Generating two sequences with one function and one parameter

I want to generate these two sequences: A = c(0.25,0.50,0.75,1,0.25,0.50,0.75,0.25,0.50,0.25) B = c(0.33,0.66,1,0.33,0.66,0.33) with one function. I already have this: X = 5 X = 4 rep(seq(1/(X-1),1,1/(X-1)), X-1) but, I still need to remove some…
3
votes
2 answers

Repeat vector elements according to values in matrix

I want to repeat each element in a vector 'A', the number of times specified in a matrix 'a'. The columns in the matrix correspond to each element in the vector. The number of repetitions to be applied are obtained from the matrix row-wise. A <-…
3
votes
1 answer

Replicate each element in a vector different times in R

Suppose I have a numeric vector v v <- 1:5 I want to rep v[1] by v[1] times. v[2] by v[2] times... and so on.... The desired output would be: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 The following does not work. Got any ideas? rep(v, each = function(x)…
geom_na
  • 258
  • 2
  • 10
3
votes
2 answers

Equivalent 'rep' of R in Pandas dataframe

I have searched for some similar questions like'equivalent R function rep in Python'. In R,rep can be used to an array or a dataframe and you can set the parameter each to specify whether you want to repeat every element or repeat the whole…
adafdwwf
  • 162
  • 3
  • 12
3
votes
2 answers

R repeating sequence add 1 each repeat

I have a workbook problem for my R class I can't figure out. I need to "write an R command that uses rep() to create a vector with elements 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7" It seems to be a repeating sequence of 1 to 4, repeating 4 times and on…
Jacob Myer
  • 479
  • 5
  • 22
3
votes
1 answer

R - Make a repetitive sequence with 'rep'

I'm wondering if there is a simpler way to make a list with, for example 10 '4', 20 '6' and 30 '3' then writing it by hand (example <- c(4,4,4,4,...)) with the function 'rep'. I know i can repeat a certain sequence n times and each by n times too,…
Pedro Lima
  • 387
  • 2
  • 14
3
votes
2 answers

Creating a sequence with a gap within a range

I am trying to create a sequence of numbers within a range, skipping every 4th number. I know it probably involves seq or seq_len somehow, but I can't seem to get it straight. The sequence should be something like this: c(1,2,3,5,6,7,9,10,11...48)
Anna
  • 55
  • 3
3
votes
2 answers

dplyr repetition within %>% operator

I am trying to use rep with dplyr but I do not fully understand why I can not make it work. My data look like this. What I want is to simply repeat dayweek by n for each id. head(dt4) id dayweek n 1 1 Friday 3 2 1 Monday 3 3 1 Saturday…
giac
  • 4,261
  • 5
  • 30
  • 59
3
votes
1 answer

why does rep() behave inconsistently with this simple R example?

I am building code to run and manage simulations of sampling events at sites that can be in one of three site cohorts. I use rep() to assign the cohort identifier (1,2, or 3) using the code below: cohort <- rep(1:n.cohorts, n.sites) I have put the…
dhd
  • 79
  • 7
3
votes
2 answers

R rep function with overwriting elements

Given a vector such as > x [1] 1 1 2 1 1 1 5 1 1 1 5 7 1 1 1 1 1 1 1 1 1 I want to replicate the elements n times --BUT-- I want the old elements to be overwritten by the replications. Using the basic rep functions gives: > rep(x,x) [1] 1 1 2 2…
pat
  • 617
  • 8
  • 17
1
2
3
11 12