Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Producing numeric sequences in R using standard patterns

Tags:

r

sequences

I am working on a project where I need to enter a number of "T score" tables into R. These are tables used to convert raw test scores into standardized values. They generally follow a specific pattern, but not one that is simple. For instance, one pattern is:

34,36,39,42,44,47,50,52,55,58,60,63,66,68,
71,74,76,79,82,84,87,90,92,95,98,100,103,106

I'd prefer to use a simple function to fill these in, rather than typing them by hand. I know that the seq() function can create a simple seqeuence, like:

R> seq(1,10,2)
[1] 1 3 5 7 9

Is there any way to create more complex sequences based on specific patterns? For instance, the above data could be done as:

c(34,seq(36:106,c(3,3,2)) # The pattern goes 36,39,42,44,47,50,52 (+3,+3,+2)

...however, this results in an error. I thought there would be a function that should do this, but all my Google-fu has just brought me back to the original seq().

like image 213
TARehman Avatar asked Jul 17 '12 21:07

TARehman


People also ask

Which function is used to generate sequences in R?

seq() function in R Language is used to create a sequence of elements in a Vector.

What is sequence how sequence can be generated in R explain with example?

The rep() method of base R is used to generate a replicated sequence from a specified vector, where each element of the vector can be repeated at any number of specified times. This method can take a character, floats, or integers-type input vectors. Arguments: seq – The vector to generate a sequence of.

How do you repeat numbers in R?

How do you Repeat a Sequence of Numbers in R? To repeat a sequence of numbers in R you can use the rep() function. For example, if you type rep(1:5, times=5) you will get a vector with the sequence 1 to 5 repeated 5 times.


2 Answers

This could be done using the cumsum (cumulative sum) function and rep:

> 31 + cumsum(rep(c(3, 2, 3), 9))
 [1]  34  36  39  42  44  47  50  52  55  58  60  63  66  68  71  74  76  79  82
[20]  84  87  90  92  95  98 100 103

To make sure sure the sequence stops at the right place:

> (31 + cumsum(rep(c(3, 2, 3), 10)))[1:28]
 [1]  34  36  39  42  44  47  50  52  55  58  60  63  66  68  71  74  76  79  82
[20]  84  87  90  92  95  98 100 103 106
like image 157
David Robinson Avatar answered Nov 16 '22 00:11

David Robinson


Here is a custom function that should work in most cases. It uses the cumulative sum (cumsum()) of a sequence, and integer division to calculate the length of the desired sequence.

cseq <- function(from, to, by){
  times <- (to-from) %/% sum(by)
  x <- cumsum(c(from, rep(by, times+1)))
  x[x<=to]
}

Try it:

> cseq(36, 106, c(3,3,2))
 [1]  36  39  42  44  47  50  52  55  58  60  63  66  68  71  74  76  79  82  84  87  90  92  95  98
[25] 100 103 106

> cseq(36, 109, c(3,3,2))
 [1]  36  39  42  44  47  50  52  55  58  60  63  66  68  71  74  76  79  82  84  87  90  92  95  98
[25] 100 103 106 108
like image 21
Andrie Avatar answered Nov 16 '22 00:11

Andrie