Writing a for loop to count every 3 scans as 1 scan
My dataset has several sightings/data points in scans and each sighting is listed by the current scan number. I want to be able to write a code that counts scan 1, 2, 3 as scan 1 and scan 4, 5, 6 as scan 2 and so on.
Here's what I have so far including my attempted result Sample dataset.
A small dataframe example:
whales <- data.frame(rubbing.beach = c(away.from.beach, away.from.beach, away.from.beach, away.from.beach, away.from.beach, away.from.beach, away.from.beach, away.from.beach, away.from.beach, away.from.beach),
scan.no.1 = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 6))
I want to make scan.no.1 column to scan.no column. I use the code below to generate the scan.no column but that is not the result I want.
Using the above scan.no.1, I want a scan.no column with the following results:
scan.no = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2).
scan.no=rep(NA, nrow(whales));
scan.no[1:3]=1;
for (i in 3:(nrow(whales)-1)) {
scan.no[i+1] = ifelse(whales$scan.no.1[i+1] != whales$scan.no.1[i],
scan.no[i]+1, scan.no[i])
}
How about this
scans <- 1:21
scan_no <- ceiling(scans/3)
scan_no
# [1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7