PADLEVEL:Activity_level
PADTIMES:times_did_activity_in_past_30_days
PADMETS:MET_score
library(dplyr)
exedata <- tibble(exedata)
exedata
exedata2 <- exedata %>% # Convert to week
mutate(times_week=times_did_activity_in_past_30_days / 30 * 7) %>%
# distinguish MET_cat <3 [3,6] >6
mutate(MET_cat=if_else(MET_score<3,"<3",if_else(MET_score>6,">6","[3,6]"))) %>%
#forcats::as_factor(MET_cat) %>%
group_by(seqn,MET_cat) %>%
summarise(times_week_sum=sum(times_week)) %>%
# moderate=[1,5]times + MET [3,6] ; vigorous=[1,3]times+ MET>6
mutate(Activity_level2=if_else(
times_week_sum<=5 & times_week_sum>=1 & MET_cat=="[3,6]","moderate",
if_else(
times_week_sum<=3 & times_week_sum>=1 & MET_cat==">6","vigorous","insufficient")))
exedata3 <- exedata2 %>%
ungroup() %>%
select(.,-c(MET_cat,times_week_sum)) %>%
mutate(values=1,ID=1:nrow(exedata2)) %>%
relocate(ID,.before = 1) %>%
tidyr::pivot_wider(., names_from = Activity_level2,
values_from = values)
exedata3=as.data.frame(exedata3)
exedata3[which(is.na(exedata3),arr.ind = T)]=0
exedata4 <- exedata3 %>%
mutate(ID=forcats::as_factor(seqn)) %>%
group_by(seqn) %>%
summarise(across(c(moderate:vigorous),~ sum(.x)))
head(exedata4)
table(duplicated(exedata4$seqn))
exedata4 <- exedata4 %>%
relocate(insufficient,.before =moderate ) %>%
mutate(activity=if_else(vigorous>=1,"vigorous",
if_else(moderate>=1,"moderate","insufficient")))