-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 17 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(yfix), y)
}
broadtime(testdat$timestamp)
testdat <- data.frame(timestamp = c("2023-05-02 03:30:00 EDT", "2023-05-02 04:15:00 EDT", "2023-05-02 07:30:00 EDT"))
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(yfix), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(y), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(yfix), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
testdat <- data.frame(timestamp = c("2023-05-02 03:30:00 EDT", "2023-05-02 04:15:00 EDT", "2023-05-02 07:30:00 EDT"))
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
hour(yfix)
yfix <- ymd_hms(testdat$timestamp, tz = "America/New_York")
hour(yfix)
hour(yfix) < 6
rollbackward(ymd_hms(yfix))
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), yfix)
ymd_hms(yfix)
rollbackward(ymd_hms(yfix))
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
mutate(y = case_when(
hour(yfix < 6) ~ rollbackward(yfix)))
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
mutate(yfix = case_when(
hour(yfix < 6) ~ rollbackward(yfix)))
}
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
rollbackward(ymd_hms(yfix))
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, newy = rollbackward(ymd_hms(yfix)), newy=y)
}
broadtime(testdat$timestamp)
testdat %>% add_column(finaltime = NA) %>%
yfix <- ymd_hms(y, tz = "America/New_York")
testdat %>% add_column(finaltime = NA) %>%
yfix <- ymd_hms(timestamp, tz = "America/New_York")
testdat %>% add_column(finaltime = NA) %>%
ymd_hms(timestamp, tz = "America/New_York")
testdat %>% add_column(finaltime = NA)
testdat %>% add_column(finaltime = NA) %>%
ymd_hms(timestamp, tz = "America/New_York")
testdat %>% add_column(finaltime = NA) %>%
ymd_hms(timestamp, tz = "America/New_York")
testdat %>% add_column(finaltime = NA) %>%
str_remove("EDT")
test2 <- data.frame(timestamp = c("2023-05-02 03:30:00", "2023-05-02 04:15:00 EDT", "2023-05-02 07:30:00 EDT"))
test2 %>% add_column(finaltime = NA) %>%
ymd_hms(timestamp, tz = "America/New_York")
test2 <- data.frame(timestamp = c("2023-05-02 03:30:00", "2023-05-02 04:15:00", "2023-05-02 07:30:00"))
test2 %>% add_column(finaltime = NA) %>%
ymd_hms(timestamp, tz = "America/New_York")
test2 %>%
ymd_hms(timestamp, tz = "America/New_York")
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(y)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
as.POSIXct(strptime(y, "%Y-%m-%d %H:%M:%S"))
t <- strftime(y, format="%H:%M:%S")
if (t < as.POSIXct("06:00:00", format="%H:%M:%S"){
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(y)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
as.Date(ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y))
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
ymd_hms(ifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y))
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y))
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
fifelse(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), y)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, rollbackward(ymd_hms(yfix)), yfix)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, rollback(ymd_hms(yfix)), yfix)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, rollback(ymd_hms(yfix)), yfix)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, yfix - 1, yfix)
}
broadtime(testdat$timestamp)
if_else(hour(yfix) < 6, yfix - days(1), yfix)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, yfix - days(1), yfix)
}
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(yfix) < 6, yfix - days(1), yfix)
}
broadtime(testdat$timestamp)
broadtime <- function(y){
yfix <- ymd_hms(y, tz = "America/New_York")
print(yfix)
if_else(hour(y) < 6, y - days(1), y)
}
broadtime(testdat$timestamp)
testdat %>% mutate(broadtime = if_else(hour(yfix) < 6, yfix - days(1), yfix))
remotes::update_packages()
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = 'C:/Users/larn_/OneDrive - Louisiana State University/Career')
df <- read.csv("C:/Users/larn_/OneDrive - Louisiana State University/Career/fitness_class_2212.csv")
df %>% select(where(is.na))
# Load necessary libraries
library(tidyverse)
install.packages("vctrs")
install.packages("vctrs")
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir = 'C:/Users/larn_/OneDrive - Louisiana State University/Career')
# Load necessary libraries
library(tidyverse)
rnorm(50)
x <- rnorm(50)
y <- rnorm(50)
dat <- as.data.frame(x, y)
dat <- data.frame(x, y)
cor(dat$x, dat$y)
cov(dat$x, dat$y)
?cor
cor.test(x, y, dat)
?cor.test
cor.test(x, y, data = dat)
install.packages("usethis")
install.packages("fs")
install.packages("here")
library(tidyverse)
library(rmarkdown)
library(usethis)
library(fs)
library(here)
use_this("rstats-wtf/wtf-explore-libraries", destdir = "C:/Users/larn_/Documents/GitHub/Workshops-Certifications/R-workflow")
use_course("rstats-wtf/wtf-explore-libraries", destdir = "C:/Users/larn_/Documents/GitHub/Workshops-Certifications/R-workflow")
# A first exploration of installed packages ------------------------------------
library(fs)
library(tidyverse)
# In which libraries does R search for packages? -------------------------------
# your personal computing environment may return one or more locations
.libPaths()
# What is your default library? ------------------------------------------------
.Library
# Confirm the library supplied is, in fact, the default library. ---------------
identical(.Library, .libPaths())
identical(.Library, .libPaths()[2])
# Huh? Maybe this is an symbolic link issue?
identical(path_real(.Library), path_real(.libPaths()))
identical(path_real(.Library), path_real(.libPaths()[2]))
# Create a tibble of all installed packages ------------------------------------
df_pkgs <- installed.packages() |>
as_tibble()
View(df_pkgs)
# How many packages are installed? ---------------------------------------------
nrow(df_pkgs)
# Explore the packages - count some things. ------------------------------------
# For example, tabulate by LibPath, Priority, or both.
df_pkgs |>
count(LibPath, Priority)
# What proportion need compilation?
df_pkgs |>
count(NeedsCompilation) |>
mutate(prop = n / sum(n))
# What version of R they were built on?
df_pkgs |>
count(Built) |>
mutate(prop = n / sum(n))
# Is every installed package either base or recommended?
df_pkgs |>
count(Priority %in% c("base", "recommended"))
# Explore package naming conventions (all lower case, contains '.', etc)
df_pkgs |>
count(str_detect(Package, "\\."))
# Use `fields` argument to installed.packages() to get more info.
# ?installed.packages
df_pkgs_url <- installed.packages(fields = "URL") |>
as_tibble()
# What proportion of packages have a URL that point to github?
df_pkgs_url |>
mutate(github = str_detect(URL, "github")) |>
count(github) |>
mutate(prop = n / sum(n))
View(df_pkgs_url)
.libPahts()
.libPaths()
.Library
?rm()
?options
?Sys.setenv
# Use a blank slate to clear environment and remove packages
usethis::use_blank_slate()
# Create a new folder to make a project with code
usethis::create_project("C:/Users/larn_/Documents/GitHub/Workshops-Certifications/R-workflow")
here::dr_here()
library(shiny)
library(dplyr)
# Import Data
setwd("C:/Users/larn_/Documents/GitHub/Workshops-Certifications/DataScienceCertification")
gym_attendance_data <- read.csv("fitness_class_2212-clean.csv")
## Build App
ui <- fluidPage(
titlePanel("Gym Attendance Tracker"),
#Create a sidebar with different filters
sidebarLayout(
sidebarPanel(
# Create a checkbox group to filter by class category
checkboxGroupInput("categories", "Class Category:",
gym_attendance_data$category,
selected = gym_attendance_data$category[1]),
# Create a slider to filter by number of months as a member
sliderInput("months_as_member", "Months as Member:", 1,
max(gym_attendance_data$months_as_member),
value = range(gym_attendance_data$months_as_member)),
# Create a drop-down menu to filter by day of the week
selectInput("days_of_week", "Day of the Week:",
c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
selected = gym_attendance_data$days_of_week[1]),
# Create a drop-down menu to filter by time of class
selectInput("time", "Time:", c("Morning", "Afternoon", "Evening"),
selected = gym_attendance_data$time[1])),
mainPanel(
# Create a table to show the filtered gym attendance data
tableOutput("gym_attendance_table"),
# Create a plot to show the overall gym attendance rate
plotOutput("gym_attendance_plot")
)
)
)
server <- function(input, output) {
#Create a reactive expression to filter the gym attendance data
filtered_gym_attendance_data <- reactive({
gym_attendance_data %>%
filter(category %in% input$categories,
months_as_member >= input$months_as_member,
days_of_week == input$days_of_week,
time == input$time)
})
#Create a reactive expression to calculate the overall gym attendance rate
overall_gym_attendance_rate <- reactive({
mean(filtered_gym_attendance_data()$attended)
})
#Create a table to show the filtered gym attendance data
output$gym_attendance_table <- renderTable({
filtered_gym_attendance_data()
})
#Create a plot to show the overall gym attendance rate
output$gym_attendance_plot <- renderPlot({
plot(overall_gym_attendance_rate(), type = "p", main = "Overall Gym Attendance Rate")
})
}
shinyApp(ui, server)
View(gym_attendance_data)
runApp('ClassAttendance')
class(gym_attendance_data$category)
unique(gym_attendance_data$category)
runApp('ClassAttendance')
runApp('ClassAttendance')
runApp('ClassAttendance')
knitr::opts_knit$set(root.dir = 'C:/Users/larn_/Documents/GitHub/Predicting-Gym-Attendance')
# Load necessary libraries
library(tidyverse)
# Load necessary libraries
library(tidyverse)
library(randomForest)
library(caret)
library(tibble)
df <- read.csv("fitness_class_2212.csv")
which(is.na(df), arr.ind=TRUE)
unique(df$months_as_member)
# Make months numeric
df$months_as_member <- as.numeric(df$months_as_member)
class(df$weight)
min(df$weight, na.rm = TRUE)
max(df$weight, na.rm = TRUE)
# Replace weight NAs with column average
df <- df %>% mutate(weight = coalesce(weight, mean(weight)))
min(df$days_before)
max(df$days_before)
class(df$days_before)
# Remove 'days' in some rows of days_before
df$days_before <- gsub("days", "", as.character(df$days_before))
# Make days_before numeric
df$days_before <- as.numeric(df$days_before)
unique(df$day_of_week)
length(df$day_of_week[which(df$day_of_week == "Fri.")])
# Make day_of_week labels consistent
df$day_of_week[which(df$day_of_week == "Monday")] <- "Mon"
df$day_of_week[which(df$day_of_week == "Wednesday")] <- "Wed"
df$day_of_week[which(df$day_of_week == "Fri.")] <- "Fri"
unique(df$time)
class(df$time)
unique(df$category)
length(df$category[which(df$category == "-")])
class(df$category)
# Replace '-' categories with 'unknown'
df$category[which(df$category == "-")] <- "unknown"
unique(df$attended)
class(df$attended)
# Make attended a factor
df$attended <- as.factor(df$attended)
# Split the data into training and testing sets
set.seed(123)
train_indices <- sample(1:nrow(df), 0.7 * nrow(df)) # 70% for training
train_data <- df[train_indices, ]
test_data <- df[-train_indices, ]
# Train the random forest model
rf_model <- randomForest(attended ~ . - booking_id, data = train_data)
View(train_data)
View(train_data)
train_data$attended <- as.factor(train_data$attended)
# Train the random forest model
rf_model <- randomForest(attended ~ . - booking_id, data = train_data)
is.na(train_data$attended)
is.na(train_data$attended) == TRUE
which(is.na(train_data$attended))
which(is.na(train_data))
# Make months numeric
df$months_as_member <- as.numeric(df$months_as_member)
class(df$weight)
min(df$weight, na.rm = TRUE)
max(df$weight, na.rm = TRUE)
# Replace weight NAs with column average
df <- df %>% mutate(weight = coalesce(weight, mean(weight)))
min(df$days_before)
max(df$days_before)
class(df$days_before)
# Remove 'days' in some rows of days_before
df$days_before <- gsub("days", "", as.character(df$days_before))
# Make days_before numeric
df$days_before <- as.numeric(df$days_before)
unique(df$day_of_week)
length(df$day_of_week[which(df$day_of_week == "Fri.")])
# Make day_of_week labels consistent
df$day_of_week[which(df$day_of_week == "Monday")] <- "Mon"
df$day_of_week[which(df$day_of_week == "Wednesday")] <- "Wed"
df$day_of_week[which(df$day_of_week == "Fri.")] <- "Fri"
unique(df$time)
class(df$time)
unique(df$category)
length(df$category[which(df$category == "-")])
class(df$category)
# Replace '-' categories with 'unknown'
df$category[which(df$category == "-")] <- "unknown"
unique(df$attended)
class(df$attended)
# Make attended a factor
df$attended <- as.factor(df$attended)
# Split the data into training and testing sets
set.seed(123)
train_indices <- sample(1:nrow(df), 0.7 * nrow(df)) # 70% for training
train_data <- df[train_indices, ]
test_data <- df[-train_indices, ]
# Train the random forest model
rf_model <- randomForest(attended ~ . - booking_id, data = train_data)
which(is.na(train_data))
View(train_data)
# Replace weight NAs with column average
df <- df %>% mutate(weight = coalesce(weight, mean(weight)))
which(is.na(df))
View(df)
which(is.na(df$weight))
View(df)
# Replace weight NAs with column average
df$weight <- df %>% mutate(weight = coalesce(weight, mean(weight)))
which(is.na(df$weight))
df %>% mutate(weight = coalesce(weight, mean(weight)))
# Replace weight NAs with column average
df <- df %>% mutate(weight = replace_na(weight, mean(weight, na.rm = TRUE)))
# Replace weight NAs with column average
df <- df %>% mutate(weight = replace_na(weight, mean(weight)))
# Replace weight NAs with column average
df <- df %>% mutate(weight = coalesce(weight, mean(weight)))
df$weight <- as.numeric(df$weight)
class(df$weight)
df$weight <- as.numeric(df$weight)
df <- read.csv("fitness_class_2212.csv")
class(df$weight)
# Replace weight NAs with column average
df <- df %>% mutate(weight = coalesce(weight, mean(weight)))
# Remove 'days' in some rows of days_before
df$days_before <- gsub("days", "", as.character(df$days_before))
# Make days_before numeric
df$days_before <- as.numeric(df$days_before)
# Make day_of_week labels consistent
df$day_of_week[which(df$day_of_week == "Monday")] <- "Mon"
df$day_of_week[which(df$day_of_week == "Wednesday")] <- "Wed"
df$day_of_week[which(df$day_of_week == "Fri.")] <- "Fri"
# Replace '-' categories with 'unknown'
df$category[which(df$category == "-")] <- "unknown"
# Make attended a factor
df$attended <- as.factor(df$attended)
which(is.na(df))
df[3115, ]
df[3212, ]
df <- na.omit(df)
which(is.na(df))
?na.omit