Skip to content
Back to formatted view

Raw Message

Message-ID: <1409200c-b1f8-51e6-0710-d7968209277b@ntu.edu.tw>
Date: 2021-01-05T07:14:54Z
From: Steven Yen
Subject: Defining partial list of variables

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"