I am trying to create a table one using the TableOne package where I have specify my variables that will be stratified. Is there a way in R to specify a list of columns if they have standardized naming convention. I.e my columns go c01, c02 all the way to c64? i.e. can i do something like vars="c01-c64" in the Table One package.
Asked
Active
Viewed 79 times
0
-
Related possible duplicate post: https://stackoverflow.com/q/5812493/680068 – zx8754 Jan 31 '21 at 22:59
-
TableOne has "vars" argument, use above post to create zero prefixed vector. – zx8754 Jan 31 '21 at 23:00
-
There are lots of ways to do this. For you example you can filter with "c %in% vars" to get the colnames with "c" in them. Check out the {stringr} and {stringi} packages for more details. – bstrain Jan 31 '21 at 23:04
1 Answers
0
Solved
list <-df[,grep("c", colnames(df))] %>% colnames()

Ronak Shah
- 377,200
- 20
- 156
- 213

RoV
- 13
- 3
-
1If you're using `%>%`, you're probably using `dplyr`, so `df %>% select(starts_with("c"))` might be nicer. – Gregor Thomas Feb 01 '21 at 00:35
-
If you just need the column names `grep("c", colnames(df), value = TRUE)` should give you that. – Ronak Shah Feb 01 '21 at 03:56