# how to randomly select 5% of the sample

The number of sequences in my sequence data is about 5,000, and the sequence length is 1440. In order to reduce computational time to obtain an optimal matching pairwise matrix, I want to randomly select 5% of the sample. I would appreciate your suggestions on that.

You can easily select a sample using the "sample" function. Here is an example using the "mvad" data set (unweighted)

## Loading the library
library(TraMineR)
## Defining sequence properties
mvad.alphabet <- c("employment", "FE", "HE", "joblessness", "school", "training")
mvad.lab <- c("employment", "further education", "higher education", "joblessness", "school", "training")
mvad.shortlab <- c("EM", "FE", "HE", "JL", "SC", "TR")
## The state sequence object.


You then use the "sample" function, specifying the number of sequences in total, and the number you want to keep (here round(0.05*nrow(mvad.seq)). If you want to be able to reproduce the sampling, you should first set the seed (i.e. initialize the random number generator) to a given (no importance) value.

set.seed(1)


If you want to use case weights, look at the "prob" argument in the help page. The sample function returns the indices of the sequences that you want to keep.

sampled.mvad.seq <- mvad.seq[sampled.indices, ]


If you want to plot the sampled sequence versus the non sampled one, you can use the following code.

sampled <- numeric(nrow(mvad.seq))
sampled[sampled.indices] <- 1
sampled[-sampled.indices] <- 0