R - type in several lines without quotes

I need to enter a row vector. Each element is a variable name that will be used in a later cycle. It's a pain to type all the quotes and commas - is there a way to just type it like this (this is what 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. 
+4
source share
4 answers

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 using a lot of shared variables like this. What not to use a vector or list? Perhaps if you better explain your workflow and what you want to do, we can offer more R-style.

You can also use paste to create a list like this:

 paste(paste0('a',rep(3:10,each=3)),rep(0:3,8),sep='_') 

EDIT after clarifying the OP, it seems he wants to filter out the 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)) 
+3
source

R does not support this style for function calls. But if you put all the variable names in a text file (one per line), for example:

 a3 a4 a5_1 a6_1 etc. 

Then you can:

 balance.vars <- scan('varnames.txt', what='') 

and you don’t need to enter all these quotes and commas.

+2
source

What about:

 balance.vars <- unlist(strsplit("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", " ")) 

or

 balance.vars <- unlist(strsplit("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", "\\W", perl=TRUE)) 

if you want to use line breaks to improve readability. :-)

+1
source

Using qdap package:

 library(qdap) qcv(terms="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") 
0
source

Source: https://habr.com/ru/post/1493696/


All Articles