{"cells":[{"metadata":{"trusted":true,"_uuid":"6f83bf2e81f3b18304446704916f088faa74f1fb"},"cell_type":"code","source":"library(data.table)\nload_data <- function(filename, ...) {\n  \n  # load json\n  json_cols <- c('device', 'geoNetwork', 'totals', 'trafficSource')    \n  json_data <- fread(filename, select=json_cols, ...)\n  \n  # remove double quotes\n  json_data[, (json_cols) := lapply(.SD, function(x) gsub('\\\"\\\"', '\\\"', x))]\n  \n  # Thank you @ML: https://www.kaggle.com/mrlong/r-flatten-json-columns\n  jsontodf <- function(col){\n    data.table(rlist::list.stack(lapply(col, function(j)\n      as.list(unlist(jsonlite::fromJSON(j)))), fill=T))\n  }                                      \n  json_data <- do.call(cbind, lapply(json_data, jsontodf))\n  \n  # load remaing data\n  other_data <- fread(filename, drop=json_cols,  ...)\n  other_data[, date := lubridate::ymd(date)]\n  cbind(other_data, json_data)\n}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ca85290f0100c3d964c218cd6432992bb9824c31"},"cell_type":"code","source":"system.time(train <- load_data('../input/train.csv', nrows=20000))\nhead(train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fe34a4521206d118f26813cbbe41932660affb03"},"cell_type":"code","source":"system.time(test <- load_data('../input/test.csv', nrows=20000))\nhead(test)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"R","language":"R","name":"ir"},"language_info":{"mimetype":"text/x-r-source","name":"R","pygments_lexer":"r","version":"3.4.2","file_extension":".r","codemirror_mode":"r"}},"nbformat":4,"nbformat_minor":1}