Skip to content

Commit 4b7b79f

Browse files
committed
add other exercises, restructure dataframes
1 parent 83522c6 commit 4b7b79f

File tree

9 files changed

+137
-56
lines changed

9 files changed

+137
-56
lines changed

R/bodyweight.R

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,36 @@
1-
#' Bicycle Crunch data
1+
#' Bodyweight Exercise Data
22
#'
33
#' @description
4-
#' Sensor-based measurements and quality ratings of athletes performing the
5-
#' Bicycle Crunch exercise, recorded across multiple sets and repetitions.
4+
#' Sensor-based measurements and quality ratings of athletes performing different
5+
#' exercises, recorded across multiple sets and repetitions at different locations
6+
#' on their bodies.
7+
#'
8+
#' Argument values for acceleration and rotation are in ms since start of the repetition.
9+
#'
10+
#' The data sets / exercises are:
11+
#'
12+
#' - `bi`: "Bicycle Crunch" (one rep is left side - right side),
13+
#' - `cr`: "Crunch",
14+
#' - `ha`: "Jumping Jack",
15+
#' - `kn`: "Squat",
16+
#' - `lu`: "Lunge",
17+
#' - `mo`: "Mountain Climber"
618
#'
719
#' @source <https://github.com/andrebert/body-weight-exercises>
820
#' @references `r format_bib("ebert2018open")`
9-
#' @format A tibble with 20,760 rows and 8 variables:
21+
#' @format A tibble with 8 variables:
1022
#' \describe{
1123
#' \item{id}{ID of the athlete.}
24+
#' \item{ex}{exercise type}
1225
#' \item{set}{The set number of the exercise.}
1326
#' \item{loc}{Location of body extremity.}
14-
#' \item{sens}{Indicator for acceleration (acc) or rotation (rot).}
15-
#' \item{dim}{Dimension of rotation.}
16-
#' \item{rep}{The number of repetitions.}
17-
#' \item{rating}{Quality of their exercise sets within a range of 1 to 5.}
18-
#' \item{activity}{The bicycle crunch activity.}
27+
#' \item{dim}{Direction of rotation / acceleration}
28+
#' \item{rep}{The count of the repetition.}
29+
#' \item{acc}{Acceleration in \eqn{m/s^2}}
30+
#' \item{rot}{Rotation in \eqn{rad/s}}
31+
#' \item{rating}{Quality of the exercise (range of 1 (best) to 5)}
1932
#' }
33+
#' @aliases bodyweight bi cr ha kn lu mo
2034
#' @examples
21-
#' bodyweight
35+
#' tibble::glimpse(bi)
2236
"bodyweight"

data-raw/bodyweight.R

Lines changed: 83 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
library(dplyr)
22
library(stringr)
33

4+
library(parallel)
5+
use_cores <- detectCores() - 2
6+
47
# source: https://github.com/andrebert/body-weight-exercises
58
path <- "~/github/body-weight-exercises"
69
files <- list.files(path, recursive = TRUE)
@@ -9,8 +12,11 @@ seg_files <- str_subset(files, "txt|INFO$", negate = TRUE)
912
info_files <- str_subset(files, "INFO$")
1013

1114
ratings <- lapply(file.path(path, info_files), function(file) {
12-
readr::read_delim(file,
13-
delim = ":", col_names = c("name", "value"), show_col_types = FALSE
15+
readr::read_delim(
16+
file,
17+
delim = ":",
18+
col_names = c("name", "value"),
19+
show_col_types = FALSE
1420
) |>
1521
filter(name == "Rating") |>
1622
pull(value) |>
@@ -23,42 +29,83 @@ bodyweight <- seg_files |>
2329
str_split("/|-", simplify = TRUE) |>
2430
as_tibble(.name_repair = "minimal") |>
2531
setNames(c("id", "ex", "set", "loc", "sens", "dim", "rep")) |>
26-
filter(ex == "bi") |>
27-
mutate(id = as.integer(id), rep = as.integer(rep))
28-
29-
bw_files <- str_subset(seg_files, "bi")
30-
bi_files <- file.path(path, bw_files)
32+
# rm participants w/o segmented exercises and exercises that were not segmented
33+
filter(!(id %in% c(5, 9, 19)) & !(ex %in% c("", "li", "ru"))) |>
34+
mutate(id = as.integer(id), rep = as.integer(rep), ex = factor(ex))
3135

32-
bi <- lapply(bi_files, function(file) {
33-
readr::read_table(file, col_names = FALSE, show_col_types = FALSE) |>
34-
t() |>
36+
get_tfd <- function(files, label) {
37+
f <- mclapply(
38+
files,
39+
function(file) {
40+
readr::read_table(file, col_names = FALSE, show_col_types = FALSE) |>
41+
t() |>
42+
as_tibble(.name_repair = "minimal") |>
43+
setNames(c("value", "arg")) |>
44+
relocate(arg, value) |>
45+
distinct(arg, .keep_all = TRUE)
46+
},
47+
mc.cores = use_cores
48+
) |>
49+
tf::tfd()
50+
files |>
51+
str_remove(paste0(path, "/")) |>
52+
str_split("/|-", simplify = TRUE) |>
3553
as_tibble(.name_repair = "minimal") |>
36-
setNames(c("value", "arg")) |>
37-
relocate(arg, value) |>
38-
distinct(arg, .keep_all = TRUE)
39-
}) |>
40-
tf::tfd()
54+
setNames(c("id", "ex", "set", "loc", "sens", "dim", "rep")) |>
55+
mutate("{{label}}" := f, rep = as.numeric(rep)) |>
56+
select(-sens)
57+
}
4158

42-
bi_ratings <- vapply(seq_len(nrow(bodyweight)), function(i) {
43-
loc <- str_c(bodyweight[i, 1:3], collapse = "/")
44-
rep <- bodyweight[i, "rep", drop = TRUE]
45-
if (is.na(ratings[loc])) NA else ratings[[loc]][rep]
46-
}, numeric(1))
59+
get_ratings <- function(ex) {
60+
ex_ratings <- ratings[str_detect(names(ratings), ex)]
61+
str_split(names(ex_ratings), "/", simplify = TRUE) |>
62+
as_tibble(.name_repair = "minimal") |>
63+
setNames(c("id", "ex", "set")) |>
64+
mutate(rating = ex_ratings) |>
65+
unnest(cols = c(rating)) |>
66+
group_by(id, ex, set) |>
67+
mutate(rep = 1:n())
68+
}
4769

48-
bodyweight <- bodyweight |>
49-
mutate(
50-
loc = factor(loc,
51-
levels = c("TL", "TR", "BL", "BR", "CH"),
52-
labels = c("left arm", "right arm", "left leg", "right leg", "chest")
53-
),
54-
sens = factor(sens,
55-
levels = c("acc", "rot"), labels = c("acceleration", "rotation")
56-
),
57-
dim = as.factor(dim),
58-
set = factor(set, levels = c("set1", "set2", "set3"), ordered = TRUE),
59-
rating = bi_ratings,
60-
activity = bi
61-
) |>
62-
select(-ex)
70+
for (this_ex in levels(bodyweight$ex)) {
71+
ex_filenames <- str_subset(seg_files, this_ex)
72+
ex_files <- file.path(path, ex_filenames)
73+
ex_acc_files <- str_subset(ex_files, "/acc-")
74+
ex_rot_files <- str_subset(ex_files, "/rot-")
75+
acc <- get_tfd(ex_acc_files, acc)
76+
rot <- get_tfd(ex_rot_files, rot)
77+
rating <- get_ratings(this_ex)
78+
ex <- full_join(acc, rot) |>
79+
right_join(rating) |>
80+
mutate(
81+
id = factor(id),
82+
ex = factor(
83+
ex,
84+
levels = c("bi", "cr", "ha", "kn", "lu", "mo"),
85+
labels = c(
86+
"Bicycle Crunch",
87+
"Crunch",
88+
"Jumping Jack",
89+
"Squat",
90+
"Lunge",
91+
"Mountain Climber"
92+
)
93+
),
94+
loc = factor(
95+
loc,
96+
levels = c("TL", "TR", "BL", "BR", "CH"),
97+
labels = c("left arm", "right arm", "left leg", "right leg", "chest")
98+
),
99+
dim = as.factor(dim),
100+
set = factor(set, levels = c("set1", "set2", "set3"), ordered = TRUE)
101+
)
102+
assign(this_ex, ex)
103+
cat(this_ex, "done\n")
104+
}
63105

64-
usethis::use_data(bodyweight, overwrite = TRUE, compress = "xz")
106+
usethis::use_data(bi, overwrite = TRUE, compress = "xz")
107+
usethis::use_data(cr, overwrite = TRUE, compress = "xz")
108+
usethis::use_data(ha, overwrite = TRUE, compress = "xz")
109+
usethis::use_data(kn, overwrite = TRUE, compress = "xz")
110+
usethis::use_data(lu, overwrite = TRUE, compress = "xz")
111+
usethis::use_data(mo, overwrite = TRUE, compress = "xz")

data/bi.rda

11.7 MB
Binary file not shown.

data/cr.rda

20.3 MB
Binary file not shown.

data/ha.rda

10.9 MB
Binary file not shown.

data/kn.rda

19.3 MB
Binary file not shown.

data/lu.rda

44.9 MB
Binary file not shown.

data/mo.rda

25 MB
Binary file not shown.

man/bodyweight.Rd

Lines changed: 30 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)