Skip to content

Instantly share code, notes, and snippets.

View ibombonato's full-sized avatar

Icaro Bombonato ibombonato

View GitHub Profile
@ibombonato
ibombonato / try_parse_date.R
Last active June 21, 2016 22:06
Parsing dates with lubridate with NA for invalid ones
library(lubridate)
mother2 <- as.data.frame(list(month=c(2,2), day=c(28,30), year=c(1988, 1988)))
mother2$birth1 <- mapply(function(y, m, d){
my_data <- paste0(as.character(y),
"-", as.character(m),
"-", as.character(d))
ifelse(is.na(lubridate::ymd(my_data, quiet = TRUE)), NA, as.POSIXct(my_data, origin="1970-01-01"))
}, y = mother2$year, m = mother2$month, d = mother2$day)
@ibombonato
ibombonato / kmeans_parallel.R
Created May 17, 2016 14:20
Run Kmeans in parallel to calculate WSS
library(doParallel)
library(foreach)
my_data <- mtcars
# Within Sum of Squares
wss <- (nrow(my_data)-1)*sum(apply(my_data,2,var))
# Number of clusters
try_clusters <- seq(from = 2, to = 8, by = 1)