Generate a repeating sequence based on vector
Use each argument:
rep(c(1027, 1028, 1030, 1032, 1037), each = 6)
# [1] 1027 1027 1027 1027 1027 1027
# [7] 1028 1028 1028 1028 1028 1028
# [13] 1030 1030 1030 1030 1030 1030
# [19] 1032 1032 1032 1032 1032 1032
# [25] 1037 1037 1037 1037 1037 1037
times argument:
rep(c(1027, 1028, 1030, 1032, 1037), times = 6)
# [1] 1027 1028 1030 1032 1037
# [6] 1027 1028 1030 1032 1037
# [11] 1027 1028 1030 1032 1037
# [16] 1027 1028 1030 1032 1037
# [21] 1027 1028 1030 1032 1037
# [26] 1027 1028 1030 1032 1037
Generate a vector of repeating values sequences, with a given number of occurrences of each value
You can do this by over-replicating the input, then removing surplus repetitions.
Edit: here is a solution with no looping:
% Repeat array to max number of possible repetitions
out = repmat( vals, 1, max(reps) );
% Implicit expansion (requires R2016b or newer, otherwise use bsxfun) to create
% matrix of reducing repetition count, then remove repeated values
% where this is <= 0, i.e. where there are no repetitions remaining.
out(reshape( reps' - (0:max(reps)-1), 1, [] ) <= 0) = [];
% Pre-R2016b version would be
% out(reshape( bsxfun(@minus, reps', (0:max(reps)-1)), 1, [] ) <= 0) = [];
Original: Requires a single loop, but over the input values not the output array so it's at least a short one...
vals = [1,2,3,4];
reps = [3,2,5,1];
out = repmat( vals, 1, max(reps) ); % repeat array to max number of possible repetitions
for ii = 1:numel(vals)
% Remove elements which have appeared too many times
out( out == vals(ii) & (cumsum(out==vals(ii)) > reps(ii)) ) = [];
end
Sort vector into repeating sequence when sequential values are missing R
We can create the order
based on the sequence of 'x'
x[order(ave(x, x, FUN = seq_along))]
#[1] 1 2 3 1 2 3 1 2 2
Or with rowid
fromdata.table
library(data.table)
x[order(rowid(x))]
#[1] 1 2 3 1 2 3 1 2 2
Create a repeating vector sequence using named vector of counts
rep( resp, times = counts)
Use the base function rep
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
R repeating sequence add 1 each repeat
We can use rep
and add with the initial vector
v1 + rep(0:3, each = length(v1))
#[1] 1 2 3 4 2 3 4 5 3 4 5 6 4 5 6 7
Or using sapply
c(sapply(v1, `+`, 0:3))
Or using outer
c(outer(v1, 0:3, `+`))
data
v1 < 1:4
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
Adding Lists Names as Plot Titles in Lapply Call in R
How to Manually Fill Colors in a Ggplot2 Histogram
Why Is Foreach() %Do% Sometimes Slower Than For
R: How to Display Clustered Matrix Heatmap (Similar Color Patterns Are Grouped)
Replacing the "Print" Function in Knitr Chunk Evaluation
K-Means Clustering in R on Very Large, Sparse Matrix
Save Object Using Variable with Object Name
How to Create an Edge List from a Matrix in R
Assign New Data Point to Cluster in Kernel K-Means (Kernlab Package in R)
Extracting Off-Diagonal Slice of Large Matrix
How to Show the Progress of Code in Parallel Computation in R
How to Convert Entire Dataframe to Numeric While Preserving Decimals
Change the Color and Font of Text in Shiny App
Jitter If Multiple Outliers in Ggplot2 Boxplot
Anti-Aliasing in R Graphics Under Windows (As Per MAC)
Is There a Reason to Prefer Extractor Functions to Accessing Attributes with $