rep

Creating a repeating vector sequence in R [duplicate]

半腔热情 提交于 2019-12-01 10:19:26
问题 This question already has an answer here : R: generate a repeating sequence based on vector (1 answer) Closed 2 years ago . I need some help. How to create the following vector sequence: 1 1 1 1 2 2 2 3 3 4 I tried to use (rep) and (seq) but still unsucessfull. 回答1: Try this: rep(1:4,4:1) Output: [1] 1 1 1 1 2 2 2 3 3 4 回答2: or less concisely: c(rep(1, 4), rep(2,3), rep(3, 2), 4) output: [1] 1 1 1 1 2 2 2 3 3 4 来源: https://stackoverflow.com/questions/46167499/creating-a-repeating-vector

R rep seq where number of rows is not multiple of seq length

家住魔仙堡 提交于 2019-12-01 01:32:38
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 2.2 2 0 .05 3.3 3 0 .45 2.8 4 1 .85 3.1 5 1 1.01 2.9 1 ... ... ... ... I've tried the following 2 ways but get an error message as it seems to only add numbers in increments of the full seq() argument: > tr2$Id1 <- rep(seq(1,5,1), (nrow(tr2)/5)) Error in `$<-.data.frame`(`*tmp*`, "Id", value = c(1, 2, 3, 4, 5, 1, 2, : replacement has 1665 rows, data has 1666 > tr2$Id1 <- rep(seq(1,5,1), (nrow(tr2)/5) +

More than one value for “each” argument in “rep” function (R) ?

耗尽温柔 提交于 2019-11-28 14:22:38
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 in order to change the number of repetition of each value, it doesn't work properly: rep (a, each = c(2,4,7)) How to solve it? Thank you in advance. well, depending on what you think the output should be, I'm guessing you want the times= parameter rep (a, times=c(2,4,7)) # [1] 2 2 4 4 4 4 6 6 6 6 6 6 6 See ?rep for the difference 来源: https:/

More than one value for “each” argument in “rep” function (R) ?

♀尐吖头ヾ 提交于 2019-11-27 08:42:15
问题 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 in order to change the number of repetition of each value, it doesn't work properly: rep (a, each = c(2,4,7)) How to solve it? Thank you in advance. 回答1: well, depending on what you think the output should be, I'm guessing you want the times=