File: toformula.R

package info (click to toggle)
r-cran-lava 1.8.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,816 kB
  • sloc: sh: 13; makefile: 2
file content (37 lines) | stat: -rw-r--r-- 888 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
##' Converts strings to formula
##'
##' Converts a vector of predictors and a vector of responses (characters) i#nto
##' a formula expression.
##'
##'
##' @param y vector of predictors
##' @param x vector of responses
##' @return An object of class \code{formula}
##' @author Klaus K. Holst
##' @seealso \code{\link{as.formula}},
##' @keywords models utilities
##' @examples
##'
##' toformula(c("age","gender"), "weight")
##'
##' @export
toformula <- function (y = ".", x = ".")
{
    xst <- x[1]
    xn <- length(x)
    if (xn > 1)
        for (i in 2:length(x)) {
            xst <- paste(xst, "+", x[i])
        }
    yst <- y[1]
    yn <- length(y)
    if (yn > 1) {
        yst <- paste0("c(", yst)
        for (i in 2:length(y)) {
            yst <- paste0(yst, ", ", y[i])
        }
        yst <- paste0(yst, ")")
    }
    ff <- paste(yst, "~", xst)
    return(as.formula(ff))
}