I constantly define variable lists from a data frame (e.g., to define a regression equation). Line 3 below does just that. Placing each variable name in quotation marks is too much work especially for a long list so I do that with line 4. Is there an easier way to accomplish this----to define a list of variable names containing "a","c","e"? Thank you! > data<-as.data.frame(matrix(1:30,nrow=6)) > colnames(data)<-c("a","b","c","d","e"); data ? a? b? c? d? e 1 1? 7 13 19 25 2 2? 8 14 20 26 3 3? 9 15 21 27 4 4 10 16 22 28 5 5 11 17 23 29 6 6 12 18 24 30 > x1<-c("a","c","e"); x1 # line 3 [1] "a" "c" "e" > x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4 [1] "a" "c" "e"
see below Steven Yen wrote/hat geschrieben on/am 05.01.2021 08:14:> I constantly define variable lists from a data frame (e.g., to define a > regression equation). Line 3 below does just that. Placing each variable > name in quotation marks is too much work especially for a long list so I > do that with line 4. Is there an easier way to accomplish this----to > define a list of variable names containing "a","c","e"? Thank you! > >> data<-as.data.frame(matrix(1:30,nrow=6)) >> colnames(data)<-c("a","b","c","d","e"); data > > a b c d e > 1 1 7 13 19 25 > 2 2 8 14 20 26 > 3 3 9 15 21 27 > 4 4 10 16 22 28 > 5 5 11 17 23 29 > 6 6 12 18 24 30 >> x1<-c("a","c","e"); x1 # line 3 > [1] "a" "c" "e" >> x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4 > > [1] "a" "c" "e" >What about: x3 <- names(data)[c(1,3,5)] x3 [1] "a" "c" "e" If I have to compile longer vectors of variable names I do it as follows: First I use: dput(names(data)) resulting in a vector of names. c("a", "b", "c", "d", "e") Then I edit the output by hand, e.g. x4 <- c("a", "b", "c", "d", "e") x4 <- c("a", "c", "e") This is especially useful with long names, where I could easily make typing errors. regards, Heinz
IMO if you want to hardcode a formula then simply hardcode a formula. If you want 20 formulas, write 20 formulas. Is that really so bad? If you want to have an abbreviated way to specify sets of variables without conforming to R syntax then put them into data files and read them in using a format of your choice. But using NSE to avoid using quotes for entering what amounts to in-script data is abuse of the language justified by laziness... the amount of work you put yourself and anyone else who reads your code through is excessive relative to the benefit gained. NSE has its strengths... but as a method of creating data objects it sucks. Note that even the tidyverse (now) requires you to use quotes when you are not directly referring to something that already exists. And if you were... you might as well be creating a formula. On January 4, 2021 11:14:54 PM PST, Steven Yen <styen at ntu.edu.tw> wrote:>I constantly define variable lists from a data frame (e.g., to define a > >regression equation). Line 3 below does just that. Placing each >variable >name in quotation marks is too much work especially for a long list so >I >do that with line 4. Is there an easier way to accomplish this----to >define a list of variable names containing "a","c","e"? Thank you! > > > data<-as.data.frame(matrix(1:30,nrow=6)) > > colnames(data)<-c("a","b","c","d","e"); data > > ? a? b? c? d? e >1 1? 7 13 19 25 >2 2? 8 14 20 26 >3 3? 9 15 21 27 >4 4 10 16 22 28 >5 5 11 17 23 29 >6 6 12 18 24 30 > > x1<-c("a","c","e"); x1 # line 3 >[1] "a" "c" "e" > > x2<-colnames(subset(data,select=c(a,c,e))); x2 # line 4 > >[1] "a" "c" "e" > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.