How to Create Vectors with Specific Intervals in R

How do you create vectors with specific intervals in R?

In R the equivalent function is seq and you can use it with the option by:

seq(from = 5, to = 100, by = 5)
# [1] 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100

In addition to by you can also have other options such as length.out and along.with.

length.out: If you want to get a total of 10 numbers between 0 and 1, for example:

seq(0, 1, length.out = 10)
# gives 10 equally spaced numbers from 0 to 1

along.with: It takes the length of the vector you supply as input and provides a vector from 1:length(input).

seq(along.with=c(10,20,30))
# [1] 1 2 3

Although, instead of using the along.with option, it is recommended to use seq_along in this case. From the documentation for ?seq

seq is generic, and only the default method is described here. Note that it dispatches on the class of the first argument irrespective of argument names. This can have unintended consequences if it is called with just one argument intending this to be taken as along.with: it is much better to use seq_along in that case.

seq_along: Instead of seq(along.with(.))

seq_along(c(10,20,30))
# [1] 1 2 3

Hope this helps.

Create a vector from a specific sequence of intervals

Let's assume you have two interval like :

interval1 <- seq(1.25, 226.250, 25)
interval2 <- seq(251, 1000, 75)

We can create a new interval combining the two and then use mapply to create sequence

new_interval <- c(as.integer(interval1), interval2)
c(mapply(`:`, new_interval, new_interval + 4))
#[1] 1 2 3 4 5 26 27 28 29 30 51 52 53 54 .....
#[89] ..... 779 780 851 852 853 854 855 926 927 928 929 930

How to build a function in R that splits variables into intervals and calculate mean, sd and a count for the intervals

This is not a function, but it achieves what you want:

# sample data
df <- data.frame(
age = runif(100, min = 10, max = 100)
)

# trying to first define the categories and then calculate the descriptive statistics
# edit: I used @thelatemail suggestion from the comments to simplify the code
df %>%
group_by(category = cut(age, c(0,30,40,50,60,Inf), labels=paste0("i",1:5))) %>%
summarise(
mean = mean(age),
sd = sd(age),
count = n()
)

Create a 24 hour vector with 5 minutes time interval in R

There is a seq.POSIXt function which has the nice property that the by argument will get parsed for "numeric interval" meaning. If you print the results with format(z, "%H%M", tz="GMT") it will appear as desired:

format( seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date()+1), by = "5 min"),
"%H%M", tz="GMT")
[1] "0000" "0005" "0010" "0015" "0020" "0025" "0030" "0035" "0040" "0045" "0050"
[12] "0055" "0100" "0105" "0110" "0115" "0120" "0125" "0130" "0135" "0140" "0145"
[23] "0150" "0155" "0200" "0205" "0210" "0215" "0220" "0225" "0230" "0235" "0240"
[34] "0245" "0250" "0255" "0300" "0305" "0310" "0315" "0320" "0325" "0330" "0335"
[45] "0340" "0345" snipped the rest.

Unless you are within 360/48 degrees of Greenwich (or is it Paris) you need to put in the tz="GMT" so that the offset for your timezone does not appear. Without that this produced a sequence starting at "1700" for me. You could assign the inner result to a name if you needed to keep it arount but it would not be a character value but rahter a POSIXct object:

z <- seq.POSIXt(as.POSIXct(Sys.Date()), as.POSIXct(Sys.Date()+1), by = "5 min")
> z[1]
[1] "2014-09-09 17:00:00 PDT"

Create an interval - Base R

Ok - I'll admit that I haven't completely understood the full utility of this, but here's a suggestion how you could do it.

Since the 'intervals' returned by cut are nothing but character vectors, you can construct them using your variables a and b, and use them to test to see if a particular interval-of-interest is present within the output of cut.

Example:
First make some intervals to search within:

set.seed(1)
mydata=sample(1:100,20)
mycutintervals=cut(mydata,7)
mycutintervals

Now construct intervals-of-interest to check if these are present:

make_interval=function(x,y) paste0("(",x,",",y,"]")

a=19
b=32
# is interval from a to b present?
make_interval(a,b) %in% mycutintervals
# [1] TRUE

c=50
# what about interval from a to c?
make_interval(a,c) %in% mycutintervals
# [1] FALSE

Define interval to create a continuous vector

It sounds like you're looking for seq:

seq(-1, 1, by = .1)
# [1] -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4
# [16] 0.5 0.6 0.7 0.8 0.9 1.0

Check whether elements of vectors are inside intervals given by matrix

We can use sapply to loop over each element of x and find if it lies in the range of any of those matrix values.

x[sapply(x, function(i) any(i > A[, 1] & i < A[,2]))]
#[1] 4 15

In case, if length(x) and nrow(A) are same then we don't even need the sapply loop and we can use this comparison directly.

x[x > A[, 1] & x < A[,2]]
#[1] 4 15

How do I generate a list with a specified increment step?

Executing seq(1, 10, 1) does what 1:10 does. You can change the last parameter of seq, i.e. by, to be the step of whatever size you like.

> #a vector of even numbers
> seq(0, 10, by=2) # Explicitly specifying "by" only to increase readability
> [1] 0 2 4 6 8 10


Related Topics



Leave a reply



Submit