mercredi 18 novembre 2015

Subset data by randomly selecting rows based on two columns

I have a large data.frame and I want to create a new data.frame with randomly selected rows based on two columns.

There are 90 unique elkIDs with ~48 rows per FixDate. I would like to make a new data.frame with 90 unique elkIDs with 4 randomly selected rows per each FixDate.

Data looks like this:

> head(df)
elkID        X       Y       Fix.Date.Time    FixDate
1   245 550345.1 4826676 2010-02-24 10:00:58 2010-02-24
2   245 550217.9 4826519 2010-02-24 10:30:47 2010-02-24
3   245 550066.3 4826478 2010-02-24 11:00:41 2010-02-24
4   245 549912.6 4826419 2010-02-24 11:30:48 2010-02-24
5   245 549977.3 4826438 2010-02-24 12:00:55 2010-02-24
6   245 549795.1 4826294 2010-02-24 12:30:29 2010-02-24

I want it to look like this (4 rows per FixDate for each unique elkID):

> df2
elkID        X       Y       Fix.Date.Time    FixDate
1   245 550345.1 4826676 2010-02-24 10:00:58 2010-02-24
2   245 550217.9 4826519 2010-02-24 10:30:47 2010-02-24
3   245 550066.3 4826478 2010-02-24 11:00:41 2010-02-24
4   245 549912.6 4826419 2010-02-24 11:30:48 2010-02-24
5   245 549977.3 4826438 2010-02-24 12:00:55 2010-02-25
6   245 549795.1 4826294 2010-02-24 12:30:29 2010-02-25

Using RStudio V0.99.467 & R3.2.1




Aucun commentaire:

Enregistrer un commentaire