mardi 20 octobre 2020

seqentially number groups based on a condition

I need some help with my R code - I've been trying to get it to work for ages and i'm totally stuck.

I have a large dataset (~40000 rows) and I need to assign group IDs to a new column based on a condition of another column. So if df$flow.type==1 then then that [SITENAME, SAMPLING.YEAR] group should be assigned with a unique group ID. This is an example:

enter image description here

This is a similar question but for SQL: Assigning group number based on condition. I need a way to do this in R - sorry I am a novice at if_else and loops. The below code is the best I could come up with but it isn't working. Can anyone see what i'm doing wrong?

thanks in advance for your help

if(flow.type.test=="0"){
  event.samp.num.test <- "1000"
} else (flow.type.test=="1"){
  event.samp.num.test <- Sample_dat %>% group_by(SITENAME, SAMPLING.YEAR) %>% tally()}

Note the group ID '1000' is just a random impossible number for this dataset - it will be used to subset the data later on.

My subset df looks like this:

> str(dummydat)
'data.frame':   68 obs. of  5 variables:
 $ SITENAME     : Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...
 $ SAMPLING.YEAR: Factor w/ 4 levels "1","2","3","4": 3 3 3 3 3 3 3 3 3 4 ...
 $ DATE         : Date, format: "2017-10-17" "2017-10-17" "2017-10-22" "2017-11-28" ...
 $ TIME         : chr  "10:45" "15:00" "15:20" "20:59" ...
 $ flow.type    : int  1 1 0 0 1 1 0 0 0 1 …

Sorry I tried dput but the output is horrendous. I have subset 40 rows of the subset data below as an example, I hope this is okay.

> head(dummydat, n=40)
   SITENAME SAMPLING.YEAR       DATE  TIME flow.type
1         A             3 2017-10-17 10:45         1
2         A             3 2017-10-17 15:00         1
3         A             3 2017-10-22 15:20         0
4         A             3 2017-11-28 20:59         0
5         A             3 2017-12-05 18:15         1
6         A             3 2017-12-06  8:25         1
7         A             3 2017-12-10 10:05         0
8         A             3 2017-12-15 15:12         0
9         A             3 2017-12-19 17:40         0
10        A             4 2018-12-09 18:10         1
11        A             4 2018-12-16 10:35         0
12        A             4 2018-12-26  6:47         0
13        A             4 2019-01-01 14:25         0
14        A             4 2019-01-05 16:40         0
15        A             4 2019-01-12  7:42         0
16        A             4 2019-01-20 16:15         0
17        A             4 2019-01-28 10:41         0
18        A             4 2019-02-03 16:30         1
19        A             4 2019-02-04 17:14         1
20        B             1 2015-12-24  6:21         1
21        B             1 2015-12-29 17:41         1
22        B             1 2015-12-29 23:33         1
23        B             1 2015-12-30  5:17         1
24        B             1 2015-12-30 17:23         1
25        B             1 2015-12-31  5:29         1
26        B             1 2015-12-31 11:35         1
27        B             1 2015-12-31 23:40         1
28        B             1 2016-02-09 10:53         0
29        B             1 2016-03-03 15:23         1
30        B             1 2016-03-03 17:37         1
31        B             1 2016-03-03 21:33         1
32        B             1 2016-03-04  3:17         1
33        B             2 2017-01-07 13:16         1
34        B             2 2017-01-07 22:24         1
35        B             2 2017-01-08  6:34         1
36        B             2 2017-01-08 11:42         1
37        B             2 2017-01-08 20:50         1
38        B             2 2017-01-31 11:39         1
39        B             2 2017-01-31 16:45         1
40        B             2 2017-01-31 22:53         1

Aucun commentaire:

Enregistrer un commentaire