My dataframe "fsp" as 1702551 obs and 3 variables. It look like this:
tibble [1,702,551 x 3]
$ date : Date[1:1702551], format: "2011-04-12" "2011-04-12" "2011-04-12" ...
$ wavelength : num [1:1702551] 350 351 352 353 354 355 356 357 358 359 ...
$ ID : chr [1:1702551] "c01" "c01" "c01" "c01" ...
Quick explanation of the data: Per each "date" and "ID" I had a spectral data (not shown) throughout the wavelength interval (350 to 2300nm). I want to create a new column "target_ID" with a sequence of repeating numers that increases to the next consecutive number each time date or ID changes. For example for the first ID, "c01" and date "2011-04-12" I will have a column with the number 1 from the wavelength 350 to 2300. The next ID will have the number 2 and so on (along the dataframe "date" changes as well)
Example of what I want to achieve (look "target_ID"):
|date |wavelength|ID |target_ID|
|:---------|:---------|:---|:--------|
|2011-04-12|350 |c01 |1 |
|2011-04-12|351 |c01 |1 |
|2011-04-12|352 |c01 |1 |
|2011-04-12|353 |c01 |1 |
|...…………………|...……………….|....|...…………….|
|2011-04-12|350 |c03 |2 |
|2011-04-12|351 |c03 |2 |
|...……………..|...……………….|....|...………………|
|2011-04-13|350 |c01 |3 |
|2011-04-13|351 |c01 |3 |
This is the code that I already tried but without success:
fsp<-fsp %>%
group_by(date, ID) %>%
mutate(target_ID, count=n())
Any help will be much appreciatted.
Thank you in advance.