commit 0710d7441e8d84c905f7a5d5b438cd2f456821c5
parent 22fc7ff24c762dc1d869285358b362bb19af04c6
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Fri, 10 May 2024 22:29:31 +0200
shorten code
Diffstat:
M | yt.R | | | 40 | +++++++++++++--------------------------- |
1 file changed, 13 insertions(+), 27 deletions(-)
diff --git a/yt.R b/yt.R
@@ -57,39 +57,25 @@ fetch.yt <- function(channel) {
return(video_dat)
}
+# fetch data -------------------------------------------------------------------
+video_dat <- list()
for (i in seq_along(channels)) {
-
- channel <- channels[i]
-
- cat(paste("Fetching:", as.character(channel), " "))
-
- video_dat <- NULL
- video_dat <- tryCatch(fetch.yt(channel), error = function(e) NULL)
-
- failed <- TRUE
- if (!is.null(video_dat)) {
- if (nrow(video_dat) > 0) {
- data <- rbind(data, video_dat)
- failed <- FALSE
- }
- }
- if (failed) {
- cat("FAILED!\n")
- } else {
- cat("OK!\n")
- }
+ cat(paste("Fetching:", as.character(channels[i]), "\n"))
+ video_dat[[i]] <- tryCatch(fetch.yt(channels[i]), error = function(e) NULL)
}
+data <- do.call(rbind, video_dat)
+
# edit data --------------------------------------------------------------------
# remove NAs
-data <- apply(X = t(data), MARGIN = 1, FUN = function(x){
- if (all(is.na(x))){
- na.omit(x) %>% as.vector()
- } else {
- x
- }
- })
+data <- apply(X = t(data), MARGIN = 1, FUN = function(x) {
+ if (all(is.na(x))){
+ na.omit(x) %>% as.vector()
+ } else {
+ x
+ }
+})
# sorting according to date and time
dates <- gsub(x = data[, 4], pattern = "-", replacement = "") %>% as.numeric()