nse and `model.matrix` ?

nse

#1

I'm trying to use nse with model.matrix . This is my reprex:

library(tidyverse)
test.df <- data.frame(someoutcome = rnorm(5), test2 = rnorm(5), somethingelse3 = rnorm(5))
str1 <- 'some'
test.df %>% model.matrix(data =  ., someoutcome ~ . -1) # this works
#>         test2 somethingelse3
#> 1  0.46570249      1.6787597
#> 2  1.32546861     -1.4879579
#> 3  0.07072583      1.1094165
#> 4 -1.03257283      0.8443111
#> 5  1.26892823      1.4716239
#> attr(,"assign")
#> [1] 1 2
test.df %>% model.matrix(data =  ., !!rlang::sym(glue::glue('{str1}outcome')) ~ . -1)
#> Error in !rlang::sym(glue::glue("{str1}outcome")): invalid argument type

Created on 2019-01-10 by the reprex package (v0.2.1)

Why do I get invalid argument type ??


#2

I will answer my own question but i'm not sure if this counts as nse:

library(tidyverse)
test.df <- data.frame(someoutcome = rnorm(5), test2 = rnorm(5), somethingelse3 = rnorm(5))
str1 <- 'some'
test.df %>% model.matrix(data =  ., as.formula(glue::glue('{str1}outcome ~ . -1')))
#>         test2 somethingelse3
#> 1  2.08337066      0.3763768
#> 2 -1.29659288     -1.4744359
#> 3  0.69308905     -0.2620991
#> 4 -0.71937465      0.4376072
#> 5  0.08443319     -0.3651641
#> attr(,"assign")
#> [1] 1 2

Created on 2019-01-10 by the reprex package (v0.2.1)

Is there a better way of doing this?


closed #3

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.