Home > Blockchain >  Is it possible to use dynamic variable inside dplyr::starts_with?
Is it possible to use dynamic variable inside dplyr::starts_with?

Time:12-19

I have a string stored in p which is the start of few column names. I want select those columns from a dataframe. But when I try the following, it is giving me only Participant.ID, Randomization. It doesn't give the columns that starts with the string in p. Can someone please help me. Many thanks

dl_pheno <- d.pio %>% select(c('Participant.ID', 'Randomization',starts_with(p)))

CodePudding user response:

No need for c()

p <- "c"

diamonds %>%  
  select(depth, starts_with(p))

# A tibble: 53,940 × 5
   depth carat cut       color clarity
   <dbl> <dbl> <ord>     <ord> <ord>  
 1  61.5  0.23 Ideal     E     SI2    
 2  59.8  0.21 Premium   E     SI1    
 3  56.9  0.23 Good      E     VS1    
 4  62.4  0.29 Premium   I     VS2    
 5  63.3  0.31 Good      J     SI2    
 6  62.8  0.24 Very Good J     VVS2   
 7  62.3  0.24 Very Good I     VVS1   
 8  61.9  0.26 Very Good H     SI1    
 9  65.1  0.22 Fair      E     VS2    
10  59.4  0.23 Very Good H     VS1    
# … with 53,930 more rows
# ℹ Use `print(n = ...)` to see more rows
  • Related