'R/tidyverse df %>% select(-c(var1, var2)) returning error

Strangely finding that running a simple df%>%select(-c(var1, var2)) is returning an error. I've never encountered this previously and have been merrily using select() many times. All packages, R itself and RStudio are up to date. Any assistance is greatly appreciated.

library(tidyverse)
library(gtsummary)
set.seed(42)
n <- 1000
dat <- data.frame(id=1:n,
                  age=sample(18:80, n, replace=TRUE),
                  sex = factor(sample(c('Male','Female'), n, rep=TRUE, prob=c(.6, .4))),
                  smoke=factor(sample(c("Never", 'Former', 'Current'), n, rep=TRUE, prob=c(.25, .6, .15))),
                  bmi=runif(n, min=16, max=45),
                  outcome1=rbinom(n = 200, size = 1, prob = 0.3),
                  outcome2=rbinom(n = 200, size = 1, prob = 0.13),
                  st=runif(n, min=24, max=60))
dat %>% select(-c(outcome2)) %>% tbl_summary(by=sex)

select error

Many thanks, Sandro



Solution 1:[1]

As @SamR said, it could be that your select() is from another package. If you run the following code, it should work:

library(tidyverse)
library(gtsummary)
set.seed(42)
n <- 1000
dat <- data.frame(id=1:n,
                  age=sample(18:80, n, replace=TRUE),
                  sex = factor(sample(c('Male','Female'), n, rep=TRUE, prob=c(.6, .4))),
                  smoke=factor(sample(c("Never", 'Former', 'Current'), n, rep=TRUE, prob=c(.25, .6, .15))),
                  bmi=runif(n, min=16, max=45),
                  outcome1=rbinom(n = 200, size = 1, prob = 0.3),
                  outcome2=rbinom(n = 200, size = 1, prob = 0.13),
                  st=runif(n, min=24, max=60))
dat %>% dplyr::select(-c(outcome2)) %>% tbl_summary(by=sex)

Output:

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Quinten