diff --git a/DESCRIPTION b/DESCRIPTION index dd6425b..4de771d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: nc Maintainer: Toby Dylan Hocking Author: Toby Dylan Hocking -Version: 2024.1.4 +Version: 2024.1.30 License: GPL-3 Title: Named Capture to Data Tables Description: User-friendly functions for extracting a data diff --git a/NEWS b/NEWS index 7a34b32..56c8525 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ +Changes in version 2024.1.30 + +- Remove code which uses old pre-measure data.table. +- Fix eurostat URL in vignette 3. + Changes in version 2024.1.4 - new vignette v7-capture-glob. diff --git a/R/capture_longer_spec.R b/R/capture_longer_spec.R index 6fb6334..6914648 100644 --- a/R/capture_longer_spec.R +++ b/R/capture_longer_spec.R @@ -29,14 +29,12 @@ capture_longer_spec <- structure(function ### data table describing a reshape longer operation. }, ex=function(){ - if("measure" %in% ls(asNamespace("data.table"))){ - (one.iris <- iris[1,]) - (single.spec <- nc::capture_longer_spec(iris, part=".*", "[.]", dim=".*", values_to="cm")) - (multiple.spec <- nc::capture_longer_spec(iris, part=".*", "[.]", column=".*")) - if(requireNamespace("tidyr")){ - tidyr::pivot_longer_spec(one.iris, single.spec) - tidyr::pivot_longer_spec(one.iris, multiple.spec) - } + (one.iris <- iris[1,]) + (single.spec <- nc::capture_longer_spec(iris, part=".*", "[.]", dim=".*", values_to="cm")) + (multiple.spec <- nc::capture_longer_spec(iris, part=".*", "[.]", column=".*")) + if(requireNamespace("tidyr")){ + tidyr::pivot_longer_spec(one.iris, single.spec) + tidyr::pivot_longer_spec(one.iris, multiple.spec) } }) diff --git a/R/capture_melt_multiple.R b/R/capture_melt_multiple.R index 2b3de42..8a3d1d4 100644 --- a/R/capture_melt_multiple.R +++ b/R/capture_melt_multiple.R @@ -29,51 +29,12 @@ capture_melt_multiple <- structure(function # Capture and melt into multiple col ### data.table::melt.data.table) ){ L <- melt_list(measure_multiple, list(...), fill=fill) - if("measure" %in% ls(asNamespace("data.table"))){ - melt( - L[["data"]], - measure.vars=L[["measure.vars"]], - na.rm=na.rm, - value.factor=FALSE, - verbose=verbose) - }else{#TO DELETE AFTER NEW data.table on CRAN. - variable_table <- attr(L[["measure.vars"]],"variable_table") - is.match <- seq_along(L[["data"]]) %in% unlist(L[["measure.vars"]]) - id.vars <- names(L[["data"]])[!is.match] - out.names <- c(id.vars, names(variable_table), names(L[["measure.vars"]])) - variable.name <- paste(out.names, collapse="") - names.dt <- data.table(variable_table) - set(names.dt, j=variable.name, value=paste(1:nrow(names.dt))) - measure.vars <- list() - missing.vec.list <- list() - for(measure.name in names(L[["measure.vars"]])){ - measure.cols <- L[["measure.vars"]][[measure.name]] - some.int <- na.omit(measure.cols)[1] - if(any(is.na(measure.cols))){ - miss.dt <- data.table() - missing.vec.list[[measure.name]] <- which(is.na(measure.cols)) - } - measure.cols[is.na(measure.cols)] <- some.int - measure.vars[[measure.name]] <- measure.cols - } - melted <- melt( - data.table(L[["data"]]), - measure.vars=measure.vars, - variable.name=variable.name, - na.rm=FALSE, - variable.factor=FALSE,#character for join. - value.factor=FALSE, - verbose=verbose) - for(rep.name in names(missing.vec.list)){ - missing.vec <- missing.vec.list[[rep.name]] - is.missing <- melted[[variable.name]] %in% missing.vec - set(melted, i=which(is.missing), j=rep.name, value=NA) - } - if(na.rm)melted <- melted[ - !apply(is.na(melted[, names(L[["measure.vars"]]), with=FALSE]), 1, any) - ] - names.dt[melted, out.names, with=FALSE, on=variable.name] - } + melt( + L[["data"]], + measure.vars=L[["measure.vars"]], + na.rm=na.rm, + value.factor=FALSE, + verbose=verbose) ### Data table of reshaped/melted/tall/long data, with a new column ### for each unique value of the capture group named "column", and a ### new column for each other capture group. diff --git a/R/capture_melt_single.R b/R/capture_melt_single.R index 22e094e..aac405a 100644 --- a/R/capture_melt_single.R +++ b/R/capture_melt_single.R @@ -24,34 +24,13 @@ capture_melt_single <- structure(function # Capture and melt into a single colum ### data.table::melt.data.table) ){ L <- melt_list(measure_single, list(...), value.name=value.name) - if("measure" %in% ls(asNamespace("data.table"))){ - melt( - L[["data"]], - measure.vars=L[["measure.vars"]], - value.name=value.name, - na.rm=na.rm, - value.factor=FALSE, - verbose=verbose) - }else{ - is.match <- seq_along(L[["data"]]) %in% L[["measure.vars"]] - variable_table <- attr(L[["measure.vars"]],"variable_table") - id.vars <- names(L[["data"]])[!is.match] - out.names <- c(id.vars, names(variable_table), value.name) - variable.name <- paste(out.names, collapse="") - names.dt <- data.table(variable_table) - set(names.dt, j=variable.name, value=names(L[["data"]])[is.match]) - tall.dt <- melt( - data.table(L[["data"]]), - id.vars=id.vars, - measure.vars=L[["measure.vars"]], - variable.name=variable.name, - value.name=value.name, - na.rm=na.rm, - variable.factor=FALSE, #character columns are preferred in joins. - value.factor=FALSE, - verbose=verbose) - names.dt[tall.dt, out.names, with=FALSE, on=variable.name] - } + melt( + L[["data"]], + measure.vars=L[["measure.vars"]], + value.name=value.name, + na.rm=na.rm, + value.factor=FALSE, + verbose=verbose) ### Data table of reshaped/melted/tall/long data, with a new column ### for each named argument in the pattern, and additionally ### variable/value columns. diff --git a/R/measure.R b/R/measure.R index a64d705..7a7a0af 100644 --- a/R/measure.R +++ b/R/measure.R @@ -8,9 +8,6 @@ measure <- structure(function cols ### Character vector, column names to match with regex. ){ - if(!"measure" %in% ls(asNamespace("data.table"))){ - stop("nc::measure only works on newer versions of data.table that include the measure function") - } var.args <- list(...) match.info <- check_names(cols, var.args) match.info[["subject.names"]] <- cols @@ -27,13 +24,11 @@ measure <- structure(function ### data.table::melt.data.table. }, ex=function(){ - if("measure" %in% ls(asNamespace("data.table"))){ - library(data.table) - iris.dt <- data.table(datasets::iris[c(1,150),]) - melt(iris.dt, measure=nc::measure(part =".*", "[.]", dim =".*")) - melt(iris.dt, measure=nc::measure(column=".*", "[.]", dim =".*")) - melt(iris.dt, measure=nc::measure(part =".*", "[.]", column=".*")) - } + library(data.table) + iris.dt <- data.table(datasets::iris[c(1,150),]) + melt(iris.dt, measure=nc::measure(part =".*", "[.]", dim =".*")) + melt(iris.dt, measure=nc::measure(column=".*", "[.]", dim =".*")) + melt(iris.dt, measure=nc::measure(part =".*", "[.]", column=".*")) }) diff --git a/man/capture_longer_spec.Rd b/man/capture_longer_spec.Rd index 7a5945e..eed1f14 100644 --- a/man/capture_longer_spec.Rd +++ b/man/capture_longer_spec.Rd @@ -24,14 +24,12 @@ there is a single output column in the reshaped \code{data}).} \examples{ -if("measure" \%in\% ls(asNamespace("data.table"))){ - (one.iris <- iris[1,]) - (single.spec <- nc::capture_longer_spec(iris, part=".*", "[.]", dim=".*", values_to="cm")) - (multiple.spec <- nc::capture_longer_spec(iris, part=".*", "[.]", column=".*")) - if(requireNamespace("tidyr")){ - tidyr::pivot_longer_spec(one.iris, single.spec) - tidyr::pivot_longer_spec(one.iris, multiple.spec) - } +(one.iris <- iris[1,]) +(single.spec <- nc::capture_longer_spec(iris, part=".*", "[.]", dim=".*", values_to="cm")) +(multiple.spec <- nc::capture_longer_spec(iris, part=".*", "[.]", column=".*")) +if(requireNamespace("tidyr")){ + tidyr::pivot_longer_spec(one.iris, single.spec) + tidyr::pivot_longer_spec(one.iris, multiple.spec) } } diff --git a/man/measure.Rd b/man/measure.Rd index a01abf4..3ff4c0a 100644 --- a/man/measure.Rd +++ b/man/measure.Rd @@ -22,12 +22,10 @@ named "column" and \code{\link{measure_single}} is called otherwise.} \examples{ -if("measure" \%in\% ls(asNamespace("data.table"))){ - library(data.table) - iris.dt <- data.table(datasets::iris[c(1,150),]) - melt(iris.dt, measure=nc::measure(part =".*", "[.]", dim =".*")) - melt(iris.dt, measure=nc::measure(column=".*", "[.]", dim =".*")) - melt(iris.dt, measure=nc::measure(part =".*", "[.]", column=".*")) -} +library(data.table) +iris.dt <- data.table(datasets::iris[c(1,150),]) +melt(iris.dt, measure=nc::measure(part =".*", "[.]", dim =".*")) +melt(iris.dt, measure=nc::measure(column=".*", "[.]", dim =".*")) +melt(iris.dt, measure=nc::measure(part =".*", "[.]", column=".*")) } diff --git a/vignettes/v3-capture-melt.Rmd b/vignettes/v3-capture-melt.Rmd index 4ba2307..c705fe0 100644 --- a/vignettes/v3-capture-melt.Rmd +++ b/vignettes/v3-capture-melt.Rmd @@ -195,7 +195,7 @@ pattern. ## Melting monthly Eurostat data Another example is [exchange rate data from -Eurostat](https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?file=data/ert_eff_ic_m.tsv.gz). +Eurostat](https://ec.europa.eu/eurostat). We first use read the entire data set into R: ```{r}