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

replication of number by other column in r

How can I use replication function in the following situation: mydata <- data.frame (var1 = 1:4, replication = c(3, 5, 3, 7)) mydata var1 replication 1 1 3 2 2 5 3 3 3 4 4 7 I want a result…
rdorlearn
  • 641
  • 4
  • 14
2
votes
2 answers

Replicate column names and splice

I have a dataframe as follows: df <- data.frame(A = c(2, 0, 1), B = c(0, 3, 2)) # A B # 1 2 0 # 2 0 3 # 3 1 2 The number in each cell indicates the times for which the corresponding column name should repeat. The replicates should be spliced by…
user18894435
  • 373
  • 1
  • 10
2
votes
3 answers

Weird behavior of mapply with rep and dplyr pipes in R

I am dealing with strings having two separators "*" and "|", and they are used in strings such as: "3\*4|2\*7.4|8\*3.2" Where the number right before "*" denotes frequency and the float or integer right after "*" denotes value. These value…
2
votes
3 answers

How to interchange a fixed string with sequential string in R?

How do I interchange a fixed string with a sequential string? For instance, if I want to repeat a pattern of a string, I would do the following: > rep(c("Filler","Model"),2) [1] "Filler" "Model" "Filler" "Model" "Filler" "Model" But, I want…
ssjjaca
  • 219
  • 1
  • 6
2
votes
4 answers

Generating an vector with rep and seq but without the c() function

Suppose that I am not allowed to use the c() function. My target is to generate the vector "1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9" Here is my attempt: rep(seq(1, 5, 1), 5) # [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4…
2
votes
2 answers

Repeating the value in a df column by a specified amount, and concatenating integer count to repeated values

I would like to use R to create an expanded_df from a template_df, where each row is repeated by a number of times specified in a separate column in the template_df, and an integer count is concatenated to the ID column in the expanded_df,…
shbrainard
  • 377
  • 2
  • 9
2
votes
0 answers

Recycle vector to specific length

I would like to simply recycle a vector to a given length (see below). The below function is inspired by OpenRepGrid::recycle, but I struggle to believe that this is the best way to get there, given that recycling is so fundamental. I am looking for…
tjebo
  • 21,977
  • 7
  • 58
  • 94
2
votes
2 answers

Repeating a numerical vector

Suppose I have the following numerical vector z = 1:3 f <-c(rep(z[1],3),rep(z[2],3),rep(z[3],3)) [1] 1 1 1 2 2 2 3 3 3 Now suppose the following: z =1:i #i is an interger How do I write a function such I obtain the same output format as in the…
Wietze
  • 109
  • 7
2
votes
2 answers

Creating Vectors sequence in R

I want to write a R program that creates the vector 0.1^3, 0.2^1, 0.1^6, 0.2^4, ..., 0.1^36, 0.2^34. v=c(seq(3,36,3)) w=c(seq(1,34,3)) x=c(0.1^v) y=c(0.2^w) z=c(x,y) Please help.
2
votes
2 answers

How do I use seq() and rep() and paste() to generate a specific vector?

I am trying to generate the following vector without using c(): a1, b2, b3, c4, c4, c6. I am having a really hard time with this. I tried to make a simple python function to help visualize it: listy = [] size = 2 lets = ["a", "b", "c"] iterator =…
2
votes
3 answers

How to select and repeat a number of rows in a dataframe in r

I believe my question is quite simple, but I can't manage to find the right answer. In any given dataframe: > data.frame(x0=c(1,2,3,4), x1=rnorm(4)) x0 x1 1 1 -0.1868765 2 2 -0.2935534 3 3 -1.3934953 4 4 0.8165035 Imagine I'd like to…
Unai Vicente
  • 367
  • 3
  • 10
2
votes
1 answer

invalid 'times' argument when using rep() function

I'm trying to add a column to a dataframe and I'm using the rep() function. Why do the first line work, but the next one doesn't? > S_Grime$Græsning=rep(1:2, 50:49) > R_Grime$Græsning=rep(1:2, 34:27) Error in rep(1:2, 34:27) : invalid 'times'…
2
votes
1 answer

What is the equivalent to the R function rep(x, each = n) in Stan?

In my Stan code, I want to add an ICAR-term (phi) to the following covariate model: // covariate models with logit link vector[total_surveys] logit_p = (X_p * beta_p) + phi[ii_sampled]; However the dim(X_p) = (1900, 3) and dim(beta_p) = 3 Thus…
2
votes
3 answers

Sample in for loop R

I'd like to sample without replacement from MU, MG, PU, PG 70 times to create a matrix (ncol=4, nrow=70) e.g. sample(c("MU","MG","PU","PG"), 4,F) sample(c("MU","MG","PU","PG"), 4,F) sample(c("MU","MG","PU","PG"), 4,F) sample(c("MU","MG","PU","PG"),…
HCAI
  • 2,213
  • 8
  • 33
  • 65
2
votes
1 answer

Repeat function on R

So my issue is the following: I'm trying to put together a list where each entry is a vector of multiple values simulated under a negative binomial distribution. I'm trying to use this code: > test <- list() > for(i in 1:100) { + test[[i]] <-…
João Carvalho
  • 159
  • 2
  • 9
1 2
3
11 12