I need to type in a vector of strings. Each element is a variable name to be used in a later loop. It's a pain to type in all the quotation marks and commas -- is there a way to just type it like so (this is something I can do in Stata):
balance.vars <- c(a3 a4 a5_1 a5_2 a5_3 a6_1 a6_2 a6_3 a6_4
a6_5 a6_6 a8 a8_1 a8_2 a8_3 a9 a9_3
a10_1 a10_2 a10_3)
etc.
You can use scan
with text
:
balance.vars <- scan(text='a3 a4 a5_1 a5_2 a5_3 a6_1 a6_2 a6_3 a6_4
a6_5 a6_6 a8 a8_1 a8_2 a8_3 a9 a9_3
a10_1 a10_2 a10_3',what='char')
But I would avoid to use many separated variables like this. What not to use a vector or a list? Maybe ifyou explain better your workflow and what do you want to do we can propose more R-style solution.
You can also use paste
to create such list, for example:
paste(paste0('a',rep(3:10,each=3)),rep(0:3,8),sep='_')
EDIT after OP clarification, it seems he wants to filter a data.frames variables.
varnames <- colnames(d)[grepl('^a[0-9]+(_[1-3])?',colnames(d))]
formulas <- paste(varnames, "group", sep = " ~ ")
res <- lapply(formulas, function(f) t.test(as.formula(f), data = d))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With