Creating a repeating vector sequence in R [duplicate] - r

This question already has an answer here:
Generate a repeating sequence based on vector
(1 answer)
Closed 5 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.

Try this:
rep(1:4,4:1)
Output:
[1] 1 1 1 1 2 2 2 3 3 4

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

Related

How can I remove one number each time while replicating a number of sequence in r? [duplicate]

This question already has answers here:
Generate an incrementally increasing sequence like 112123123412345
(4 answers)
Closed 1 year ago.
I have a sequence of numbers like this:
> seq(2,6,1)
[1] 2 3 4 5 6
I would like to replicate this sequence and remove one number from the end of the sequence while doing replication. This is what I want to have:
[1] 2 3 4 5 6 2 3 4 5 2 3 4 2 3 2
Is there any functions in R can help me get this sequence?
Thank you very much.
I am sure there is a fancier way, but the following code achieves the goal in base R.
out = integer()
a = 2:6
while( length(a) > 1 ) {
a = a[-length(a)]
out = c(out, a)
}
out
#> [1] 2 3 4 5 2 3 4 2 3 2
Created on 2021-03-18 by the reprex package (v1.0.0)
That should be it:
sequence(5:1, from = 2)
[1] 2 3 4 5 6 2 3 4 5 2 3 4 2 3 2

R Repeat Rows Data Table [duplicate]

This question already has answers here:
Repeat each row of data.frame the number of times specified in a column
(10 answers)
Closed 2 years ago.
library(data.table)
dataHAVE=data.frame("student"=c(1,2,3),
"score" = c(10,11,12),
"count"=c(4,1,2))
dataWANT=data.frame("student"=c(1,1,1,1,2,3,3),
"score"=c(10,10,10,10,11,12,12),
"count"=c(4,4,4,4,1,2,2))
setDT(dataHAVE)dataHAVE[rep(1:.N,count)][,Indx:=1:.N,by=student]
I have data 'dataHAVE' and seek to produce 'dataWANT' that basically copies each 'student' 'count' number of times as shown in 'dataWANT'. I try doing this as shown above in data.table as this is the solution I seek but get error
Error: unexpected symbol in "setDT(dat)dat"
and I cannot resolve thank you so much.
Try:
setDT(dataHAVE)[rep(1:.N,count)]
Output:
student score count
1: 1 10 4
2: 1 10 4
3: 1 10 4
4: 1 10 4
5: 2 11 1
6: 3 12 2
7: 3 12 2
As explained you could also replace 1:.N and do setDT(dataHAVE)[dataHAVE[, rep(.I, count)]].
Just FYI, there's also a nice function in tidyr that does similar thing:
tidyr::uncount(dataHAVE, count, .remove = FALSE)
Here is a base R solution
dataWANT<-do.call(rbind,
c(with(dataHAVE,rep(split(dataHAVE,student),count)),
make.row.names = FALSE))
such that
> dataWANT
student score count
1 1 10 4
2 1 10 4
3 1 10 4
4 1 10 4
5 2 11 1
6 3 12 2
7 3 12 2

Identify repetitive pattern in numeric vector in R with fuzzy search [duplicate]

This question already has answers here:
Find and break on repeated runs
(3 answers)
Closed 6 years ago.
Imagine a vector of integers like so:
> rep(c(1,4,2),10)
[1] 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2 1 4 2
For us human beings it seems easy to identify the pattern 1 - 4 - 2 even without knowing the function how the vector was created. But how would you identify this pattern using R?
Edit
As this question was marked as a dupe I'm going to specify it a bit. The above example was an easy one to explain the idea. The main goal would be to identify more hidden patterns like 1 4 2 5 6 7 1 4 2 9 1 4 2 3 4 5 1 4 2 and also patterns that are approximately the same like 1 4 2 1 4 1.99 1 4 2 1.01 4 2 1 4.01 2. What are the ideas to always Identify the pattern 1 4 2 in those cases?
Assuming that the subpattern must start at the beginning and repeat to the end of the input try it for a subpattern length of k = 1, 2, 3, ... We have assumed that only patterns that are half the length of the input or less are to be considered:
for(k in seq_len(length(x)/2)) {
pat <- x[1:k]
if (identical(rep(pat, length = length(x)), x)) {
print(pat)
break
}
}
## [1] 1 4 2
Note: This was used as the input x:
x <- rep(c(1, 4, 2), 10)

Subtract from the previous row R [duplicate]

This question already has answers here:
How to find the difference in value in every two consecutive rows in R?
(4 answers)
Closed 7 years ago.
I have a dataframe like so:
df <- data.frame(start=c(5,4,2),end=c(2,6,3))
start end
5 2
4 6
2 3
And I want the following result:
start end diff
5 2
4 6 1
2 3 -1
Essentially it is:
end[2] (second row) - start[1] = 6-5=1
and end[3] - start[2] = 3-4 = -1
What is a good way of doing this in R?
Just a simple vector subtraction should work
df$diff <- c(NA,df[2:nrow(df), 2] - df[1:(nrow(df)-1), 1])
start end diff
1 5 2 NA
2 4 6 1
3 2 3 -1
library(data.table)
setDT(df)[,value:=end-shift(start,1,type="lag")]
start end value
1: 5 2 NA
2: 4 6 1
3: 2 3 -1

Create a vector that repeats itself in R

I would like to create a vector that repeats itself. (eg 1:3 until 12 rows)
1,2,3,1,2,3,1,2,3,1,2,3
How can I do this in R?
Thanks for your help.
See ?rep. What you want is as easy as
> rep(1:3, times = 4)
[1] 1 2 3 1 2 3 1 2 3 1 2 3
but if you don't know the length of the vector until run time but you do know the length of the output required, you could do (updated to reflect comment from #baptiste):
> rep(1:3, length.out = 12)
[1] 1 2 3 1 2 3 1 2 3 1 2 3

Resources