Skip to content

Commit 51ae00c

Browse files
author
maechler
committed
generalize reformulate() such that drop.terms(terms( ~ x), 1) works
git-svn-id: https://svn.r-project.org/R/trunk@87779 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent e299f16 commit 51ae00c

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

doc/NEWS.Rd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,10 @@
608608
desirable according to option \code{max.print}, or argument
609609
\code{max}, respectively, addressing most of the remaining part of
610610
\PR{15027}, thanks to \I{Sherry Zhang}'s patch.
611+
612+
\item \code{drop.terms(y ~ w, 1)} and similar now work, thanks to
613+
\I{Benjamin Sommer}'s report in \PR{18861} and collaboration with
614+
\I{Heather Turner} improving \code{reformulate()}.
611615
}
612616
}
613617
}

src/library/stats/R/models.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# File src/library/stats/R/models.R
22
# Part of the R package, https://www.R-project.org
33
#
4-
# Copyright (C) 1995-2024 The R Core Team
4+
# Copyright (C) 1995-2025 The R Core Team
55
#
66
# This program is free software; you can redistribute it and/or modify
77
# it under the terms of the GNU General Public License as published by
@@ -183,8 +183,9 @@ delete.response <- function (termobj)
183183
reformulate <- function (termlabels, response=NULL, intercept = TRUE, env = parent.frame())
184184
{
185185
## an extension of formula.character()
186-
if(!is.character(termlabels) || !length(termlabels))
187-
stop("'termlabels' must be a character vector of length at least one")
186+
if(!is.character(termlabels))
187+
stop("'termlabels' must be a character vector")
188+
if(intercept && !length(termlabels)) termlabels <- "1"
188189
termtext <- paste(termlabels, collapse = "+")
189190
if(!intercept) termtext <- paste(termtext, "- 1")
190191
terms <- str2lang(termtext)

src/library/stats/man/delete.response.Rd

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/stats/man/delete.response.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2024 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{delete.response}
@@ -11,6 +11,8 @@ delete.response(termobj)
1111
reformulate(termlabels, response = NULL, intercept = TRUE, env = parent.frame())
1212

1313
drop.terms(termobj, dropx = NULL, keep.response = FALSE)
14+
15+
termobj[i]
1416
}
1517
\alias{reformulate}
1618
\alias{drop.terms}
@@ -19,13 +21,13 @@ drop.terms(termobj, dropx = NULL, keep.response = FALSE)
1921
\arguments{
2022
\item{termobj}{a \code{\link[=terms.object]{terms}} object.}
2123
\item{termlabels}{character vector giving the right-hand side of a
22-
model formula. Cannot be zero-length.}
24+
model formula. May be zero-length.}
2325
\item{response}{a character string, symbol or call giving the left-hand
2426
side of a model formula, or \code{NULL}.}
2527
\item{intercept}{logical: should the formula have an intercept?}
2628
\item{env}{the \code{\link{environment}} of the \code{\link{formula}}
2729
returned.}
28-
\item{dropx}{a numeric vector indexing \code{labels(termobj)}
30+
\item{dropx, i}{a numeric vector indexing \code{labels(termobj)}
2931
(that is, the \code{"term.labels"} attribute of \code{termobj}),
3032
indicating terms to be dropped from the right-hand side of the
3133
model, or \code{NULL} (default) to keep all terms.}
@@ -48,10 +50,10 @@ drop.terms(termobj, dropx = NULL, keep.response = FALSE)
4850
back compatibly, with a deprecation warning.
4951
}
5052
\value{
51-
\code{delete.response} and \code{drop.terms} return a \code{terms}
53+
\code{delete.response} and \code{drop.terms} return a \code{\link{terms}}
5254
object.
5355

54-
\code{reformulate} returns a \code{formula}.
56+
\code{reformulate} returns a \code{\link{formula}}.
5557
}
5658
\seealso{\code{\link{terms}}}
5759

tests/reg-tests-1e.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,6 +1853,19 @@ stopifnot(identical(print(quantile(x, 0.7, type=2)),
18531853
## was 973 in R <= 4.4.x
18541854

18551855

1856+
## drop.terms(<"one" term>, 1) should work, PR#18861
1857+
stopifnot(exprs = {
1858+
identical( ~ 1, reformulate(character()))
1859+
identical( ~ -1, reformulate(character(), intercept = FALSE))
1860+
identical(terms( ~ 1 ), drop.terms(terms(y ~ 1 + w), 1))
1861+
identical(terms( ~ 1 ), drop.terms(terms(y ~ w), 1))
1862+
identical(terms( ~ -1), drop.terms(terms(y ~ w - 1), 1))
1863+
identical(terms( ~ -1), drop.terms(terms(y ~ w + 0), 1))
1864+
identical(terms(y ~ 1), drop.terms(terms(y ~ w), 1, keep.response = TRUE))
1865+
})
1866+
## all these used to error in reformulate() in R < 4.5.0
1867+
1868+
18561869

18571870
## keep at end
18581871
rbind(last = proc.time() - .pt,

0 commit comments

Comments
 (0)