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
0
votes
1 answer

Generating a matrix of ascending values, after every 2 rows, without a for loop in R

I'm trying to generate this matrix without using a for loop (to speed up the processing time): Module 1 Module 2 Module 3 y0 20 20 20 y1 20 20 20 y0 40 40 40 y1 40 40 …
MBorg
  • 1,345
  • 2
  • 19
  • 38
0
votes
1 answer

Generating random numbers in two vectors in R given a specified condition

I want to create two vectors in R that contain values randomly drawn from a uniform distribution given a specified condition, that is for example if the number in vector A is < 50 then the number in vector B should be greater than 50. I use this…
0
votes
1 answer

constructing future coupon payment dates for bonds

I have a Problem and would be very grateful if you could help me. starting Situation: Bonds: "026351AZ9" "026351BC9" First Coupon Date of These Bonds: "2029-02-15" "2010-09-11" (class date) Count of the years where the bonds pay coupons: 3 years…
I.AH
  • 27
  • 1
  • 7
0
votes
1 answer

Filling a NA-Vector multiple times with the same vector [day number of the year]

I have the following solution, which works, but creates an error message. day.number.year <- (1:33810)*NA day.number.year[] <- rep(seq(1,360, by = 1), times = 94) Warning message: In day.number.year[] <- rep(seq(1, 360, by = 1), times =…
Nucore
  • 107
  • 1
  • 13
0
votes
1 answer

How to get the factorial using rep statement in r?

I want to get the factorial function with rep statement. I try!! But failed!! fact <- function(n){ for (i in 0:n){ while(i
0
votes
4 answers

R repeat a seqeunce reducing max value by 1

I have a sequence of days numbered from 0 (start) to say 5 (last day) I would like to create a list that repeats N times where N is number of days total, in this case it is 6 days (0,1,2,3,4,5), each consecutive repetition should reduce the length…
user3674993
  • 129
  • 1
  • 9
0
votes
2 answers

Repeating elements of one vector according to a second

In MATLAB, if N = 2 this is the line I need: M = [V(1)*ones(1,L(1)) V(2)*ones(1,L(2))]; If N = 3, the line is: M = [V(1)*ones(1,L(1)) V(2)*ones(1,L(2)) V(3)*ones(1,L(3))]; How could I write a line producing the same results for an arbitrary N?
baister
  • 283
  • 2
  • 9
0
votes
2 answers

Create vector = (0 1 1 0 0 0 1 1 1 1)?

How can I create the following vector? vec = (0 1 1 0 0 0 1 1 1 1) I already tried rep(0:1,times=1:4) which works with numbers other than 0 but does not here...=
letter
  • 1
  • 1
0
votes
2 answers

In R: how to simulate the season of an NBA team?

I basically want to create a vector which length is 82. This should contain either "W" or "L", but their occurrences should be randomly determined. I tried with this season<-rep(c("W","L"), times = 1, length.out = 82, each = 1) but I only get this:…
FaCoffee
  • 7,609
  • 28
  • 99
  • 174
0
votes
1 answer

Repeat values in a matrix (R)

Here is the code I am working with: A <- matrix(1:9, nrow = 3) A cbind(A,A,A) This gives an output: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [1,] 1 4 7 1 4 7 1 4 7 [2,] 2 5 8 2 5 8 2 5 …
Adam Warner
  • 1,334
  • 2
  • 14
  • 30
0
votes
0 answers

Generating the input in a known sequence

I am interested in generating the input for the example mentioned in this question. I have generated the input using the below snippet data.frame(grade = rep(LETTERS[1:5],4,rep=TRUE), sex = c( rep(c('male','female'), each=5),…
Prradep
  • 5,506
  • 5
  • 43
  • 84
0
votes
1 answer

setCellStyle - apply cell style (percent) to matrix using XLConnect

My question is based on an issue mentioned in a previous question Formatting of numbers using Mirai's XLConnect. I have trouble implementing this solution from @joran and I think I might not be the only person with this problem. I want to export a…
rmuc8
  • 2,869
  • 7
  • 27
  • 36
0
votes
3 answers

Sort R rows based on the number of repetition

I have a table like: me mine 1 z ghm 2 d gwm 3 d gom 4 d gum 5 f gom 6 g gum 7 h gom 8 t ghm 9 y gom 10 u gom how can I sort these data based on the…
0
votes
2 answers

add rows to group in df

I have a data.frame with varying numbers of rows per person. If a person has less than 4 rows I would like to give them 4 rows. I would like the extra rows to be a replicate of the the last row per person e.g., Tom below. I know how to repeat rows…
user2363642
  • 727
  • 9
  • 26
0
votes
1 answer

How to create an indexed vector base on another vector?

I'm trying to create an indexed vector based on another vector. Here is the source vector: a <- c("A", "A", "B", "C", "D", "E", "E", "E") and the resulting indexing vector should look like this: x <- c(1, 2, 1, 1, 1, 1, 2, 3) I tried this, but…
jrara
  • 16,239
  • 33
  • 89
  • 120
1 2 3
11
12