Generate a sequence of numbers with repeated intervals
I find the sequence
function to be helpful in this case. If you had your data in a structure like this:
(info <- data.frame(start=c(1, 144, 288), len=c(6, 6, 6)))
# start len
# 1 1 6
# 2 144 6
# 3 288 6
then you could do this in one line with:
sequence(info$len) + rep(info$start-1, info$len)
# [1] 1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293
Note that this solution works even if the sequences you're combining are different lengths.
Generate a irregular sequence of numbers based on a vector of intervals in R
c(start + cumsum(c(0, intervals)), end)
# [1] -100 -90 -85 15 19 100
Then "end" is just tacked on the end. You can do something else with it, but you'll have to decide what would happen in the case your intervals go over it.
Also note that your stated requirement,
I want the length of my sequence to be same length as the interval vector.
is contradicted by your desired output. If you want different behavior, you'll need to be more precise.
Create sequence of repeated values, in sequence?
You missed the each=
argument to rep()
:
R> n <- 3
R> rep(1:5, each=n)
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
R>
so your example can be done with a simple
R> rep(1:8, each=20)
Repeating a repeated sequence
You can do it with a single rep
call. The each
and times
parameters are evaluated sequentially with the each
being done first.
rep(1:4, times=3, each=3) # 'each' done first regardless of order of named parameters
#[1] 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4 1 1 1 2 2 2 3 3 3 4 4 4
Sequence with repeating numbers
Another option
library(dplyr)
df %>%
mutate(pair = as.integer(gl(n(), 2, n())))
# id color pair
#1 1 red 1
#2 2 red 1
#3 3 red 2
#4 4 red 2
#5 5 red 3
#6 6 blue 3
#7 7 blue 4
#8 8 blue 4
#9 9 blue 5
#10 10 blue 5
Or with rep
and cumsum
df %>%
mutate(pair = cumsum(rep(c(TRUE, FALSE), length.out = n())))
Or much simpler case with base R
df$pair <- c(TRUE, FALSE)
df$pair <- cumsum(df$pair)
Generate sequence of repeating, ascending integers, using a list containing the number of repetitions for each, with thrust
You can do this purely with thrust, using an approach similar to yours.
- Do a prefix sum on the input to determine size of result for step 2, and scatter indices for step 3
- Create an output vector to hold the result
- scatter ones to the appropriate locations in the output vector, given by the indices from step 1
- do a prefix sum on the output vector.
Note that this method would have to be modified if the input reps vector is allowed to contain values of 0.
Here's a worked example:
$ cat t404.cu
#include <thrust/scan.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <iostream>
int main(){
int host_reps[] = {3, 2, 5, 1};
int ds = sizeof(host_reps)/sizeof(int);
thrust::device_vector<int> reps(host_reps, host_reps+ds);
thrust::inclusive_scan(reps.begin(), reps.end(), reps.begin());
thrust::device_vector<int> result(reps[reps.size()-1]);
thrust::copy_n(thrust::constant_iterator<int>(1), reps.size()-1, thrust::make_permutation_iterator(result.begin(), reps.begin()));
thrust::inclusive_scan(result.begin(), result.end(), result.begin());
thrust::copy_n(result.begin(), result.size(), std::ostream_iterator<int>(std::cout, ","));
std::cout << std::endl;
}
$ nvcc -o t404 t404.cu
$ ./t404
0,0,0,1,1,2,2,2,2,2,3,
$
Related Topics
Linear Model with 'Lm': How to Get Prediction Variance of Sum of Predicted Values
How to Adjust the Font Size of Tablegrob
Rename Columns in Multiple Dataframes, R
Trouble Installing and Loading Rjava on MAC El Capitan
How to Calculate the Distance Between Latitude and Longitude Along Rows of Columns in R
Generating Non-Duplicate Combination Pairs in R
Randomly Sample Data Frame into 3 Groups in R
Difference Between [] and $ Operators for Subsetting
Wrong Order of Y Axis in Ggplot Barplot
How to Apply a Gradient Fill to a Geom_Rect Object in Ggplot2
How to Always Display 3 Decimal Places in Datatables in R Shiny
Why Does Lm Run Out of Memory While Matrix Multiplication Works Fine for Coefficients
Predict() with Arbitrary Coefficients in R
Does Installing Blas/Atlas/Mkl/Openblas Will Speed Up R Package That Is Written in C/C++
Matrix Display Without Row and Column Names
How to Convert a Character String Date to Date Class If Day Value Is Missing
R: Further Subset a Selection Using the Pipe %>% and Placeholder