samedi 13 mai 2017

subset random quarter time data from a lubridate dataset

How can you use match() function to generate random quarter(like Jan,Feb,March) and select a subset of your data: rows corresponding to generated random quarter and the 1st and 3rd column.

Assume we have a 12rows and 3columns time data, something like this:

      dates     strings    numbers
1  2017-01-31       S       5
2  2017-02-28       T       1
3  2017-03-31       C       8
4  2017-04-30       Q       2
5  2017-05-31       D       6
6  2017-06-30       X      12
7  2017-07-31       N      10
8  2017-08-31       Y       9
9  2017-09-30       P      11
10 2017-10-31       G       4
11 2017-11-30       F       7
12 2017-12-31       R       3

I mean it's easy to select random 3 rows in the dataset and subset only the column 1 and 3. like this,

df[sample(nrow(df), 3),c("dates","numbers")]

Therefore, i have,

        dates    numbers
5  2017-05-31       1
9  2017-09-30       4
11 2017-11-30      12

But how can you get random q1 by using match() to achieve like this,

        dates    numbers
1  2017-01-31       5
2  2017-02-28       1
3  2017-03-30       8




Aucun commentaire:

Enregistrer un commentaire