mardi 14 avril 2020

Sorting data via if statement in R

I have a large CSV of workout data extracted from GPX files consisting of 6 columns:

1. No (e.g, (1 through ~900 thousand)

2. latitude (e.g., 34.105329,

3. longitude (e.g., -118.299236),

4. elevation (in meters,

5. date (e.g., 10/20/2017),

6. time (2:08:05 AM)

I would like to establish a column that notes the workout number, e.g., workout 1 encompasses rows 1 through 2000 and workout 2 encompasses rows 2001 through 5000. I was able to accomplish in Excel with an If statement, but have not figured out how to accomplish this in R.

Excel Snapshot with Code Snippet

Basically if a data point was recorded on the same day AND within two hours of the preceding data point, both points belonged to the same workout. If data points were logged in the same day but were separated by more than 2 hours they belong to two separate workouts. I've pasted some data below that include the first few rows of Workout 1 and the first few rows of Workout 2 (just enough to demonstrate how the Excel formula works).

Dput Code:

dput(droplevels(mydata[1:10, ]))

Dput Output:

structure(list(No = 1:10, Latitude = c(34.092483, 34.092534, 
34.092573, 34.092624, 34.092652, 34.092684, 34.092712, 34.092742, 
34.092774, 34.092808), Longitude = c(-118.300414, -118.300448, 
-118.300434, -118.300431, -118.300428, -118.300425, -118.300423, 
-118.300425, -118.300426, -118.300427), Altitude = c(104.2, 104.2, 
104.3, 104.4, 104.4, 104.5, 104.5, 104.5, 104.6, 104.6), Date = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "10/20/2017", class = "factor"), 
    Time = structure(1:10, .Label = c("1:40:18", "1:43:06", "1:43:08", 
    "1:43:10", "1:43:11", "1:43:12", "1:43:13", "1:43:14", "1:43:15", 
    "1:43:16"), class = "factor")), row.names = c(NA, 10L), class = "data.frame")

Data Sample:

No  Latitude    Longitude   Altitude    Date    Time    Workout#
1   34.092483   -118.300414 104.2   10/20/2017  1:40:18 1
2   34.092534   -118.300448 104.2   10/20/2017  1:43:06 1
3   34.092573   -118.300434 104.3   10/20/2017  1:43:08 1
4   34.092624   -118.300431 104.4   10/20/2017  1:43:10 1
5   34.092652   -118.300428 104.4   10/20/2017  1:43:11 1
1332    34.092487   -118.300577 104.1   11/4/2017   1:23:24 2
1333    34.092513   -118.300565 104.2   11/4/2017   1:23:25 2
1334    34.09255    -118.30053  104.3   11/4/2017   1:23:26 2
1335    34.092592   -118.300495 104.4   11/4/2017   1:23:28 2
1336    34.092619   -118.300481 104.4   11/4/2017   1:23:29 2
1337    34.092668   -118.300467 104.5   11/4/2017   1:23:31 2

Aucun commentaire:

Enregistrer un commentaire