I have the following block of code that needs to be repeated often:
flights <- fread("https://raw.githubusercontent.com/wiki/arunsrinivasan/flights/NYCflights14/flights14.csv")
flights$origin %>% table()
flights[grepl("jfk", origin, ignore.case = TRUE),
origin := "0",
][grepl("ewr|lga", origin, ignore.case = TRUE),
origin := "1",
][, origin := as.numeric(origin)]
flights$origin %>% table()
Here is my attempt at wrapping this in a function that allow me to have n
number of regex expressions and replacements for those for any given column in the data set.
my_function <- function(regex, replacement, column) {
flights[, column, with = FALSE] %>% table()
for (i in seq_along(regex)) {
responses[grepl(regex[i], column, ignore.case = TRUE),
column := replacement[i],
with = FALSE]
}
flights[, column := as.numeric(column)]
flights[, column, with = FALSE] %>% table()
}
But this spits the following warning message:
Warning messages:
1: In `[.data.table`(flights, grepl(regex[i], column, ignore.case = TRUE), :
with=FALSE together with := was deprecated in v1.9.4 released Oct 2014. Please wrap the LHS of := with parentheses; e.g., DT[,(myVar):=sum(b),by=a] to assign to column name(s) held in variable myVar. See ?':=' for other examples. As warned in 2014, this is now a warning.
2: In eval(jsub, SDenv, parent.frame()) : NAs introduced by coercion
Any help would be appreciated. Many thanks.