Pass expression as argument in R Survey package
Asked Answered
B

2

8

I have a question regarding calling variables in svycontrast() function with survey package. I'm trying to automate some contrast against a fixed parameter. I can do that no problem like this:

library(survey)    
data(api)

dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)

diff <- svyby(~enroll, by = ~cnum, dclus1, na.rm.all = FALSE, svymean, covmat = T, vartype = "se")

parameter <- 550

svycontrast(diff, quote(`1` - parameter))

#           nlcon SE
# contrast 2.8182  0

However, I have been for hours trying to figure out how to call that rowname `1`, but with different approaches I keep getting mostly the following error message:

row <- quote(1)

svycontrast(diff, quote(row - parameter))
Error in row - parameter : non-numeric argument to binary operator

Any help would be very much appreciated.

Boarer answered 2/6, 2021 at 20:42 Comment(1)
I modified the title to attract more attention ;-)Champ
G
7

I think you can use bquote instead of quote here

> row <- 1

> svycontrast(diff, bquote(.(as.name(row)) - parameter))
          nlcon SE
contrast 2.8182  0
Gull answered 5/6, 2021 at 10:33 Comment(0)
C
7

You could use substitute combined with as.symbol:

row <- 1

substitute(row - parameter, list(row = as.symbol(row)))
# `1` - parameter

svycontrast(diff, substitute(row - parameter, list(row = as.symbol(row))))

#          nlcon SE
#contrast 2.8182  0
Champ answered 5/6, 2021 at 5:46 Comment(3)
It was really difficult to choose a right answer between yours and @ThomasisCoding since both use very similar functions. I just chose the more succint one.Boarer
Also upvoted @Gull : gave me the opportunity to discover bquoteChamp
Your answer is nice as well, upvoted already :)Gull
G
7

I think you can use bquote instead of quote here

> row <- 1

> svycontrast(diff, bquote(.(as.name(row)) - parameter))
          nlcon SE
contrast 2.8182  0
Gull answered 5/6, 2021 at 10:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.