Mary
Mary

Reputation: 67

Select rows, columns, and another group of columns based on vector

How to select specific rows; specific columns names; and another group of specific columns names based on a vector?

My data:

#my data
ID<-c(1,2,3,4,5,6)
Month<-c('Jan','Jan','Mar','Feb','Mar','Jan')
Number<-c(6,5,4,3,2,1)
Color<-c('Red','Red','Blue','Green','Green','Purple')
Q1<-c(0,1,NA,1,0,1)
Q2<-c(1,1,NA,1,NA,1)
Q3<-c(NA,0,0,1,0,1)

mydata<-cbind.data.frame(ID,Month,Number,Color,Q1,Q2,Q3)

#my vector:
Jan.vector<-c('Q1','Q3')

I have (mydata):

enter image description here

  1. I want to select the rows where Month is 'Jan'
  2. And select column names 'ID' and 'Month'
  3. And select column names that are in the vector 'Jan.vector'. I cannot list the actual columns names because they will change.
  4. I want the resulting rows and columns only, all in one dataset as below:

I want (mydata2):

enter image description here

(Background: My actual dataset has hundreds of columns, and and I will need to select columns based on the vector that will change monthly (i.e., Q1, Q3...). The vector will be the only thing that changes. I will not know the question numbers needed and will need to rely on the vector to select the question numbers. The column order will change, as well).

Upvotes: 0

Views: 64

Answers (1)

Zahiro Mor
Zahiro Mor

Reputation: 1718

if I understood you correctly:

> monthNum <- 1
> wantedCol  <-  c('ID','Month','Color','Q1','Q3') 
> mydata[ mydata$Month==substring(month.name[monthNum],1,3),match(wantedCol,names(mydata))]
  ID Month  Color Q1 Q3
1  1   Jan    Red  0 NA
2  2   Jan    Red  1  0
6  6   Jan Purple  1  1

or - you can state the month or months by:

> monthName  <-  c('Jan','Mar')
> wantedCol  <-  c('ID','Month','Color','Q1','Q3') 
> mydata[ mydata$Month %in% monthName,match(wantedCol,names(mydata))]
  ID Month  Color Q1 Q3
1  1   Jan    Red  0 NA
2  2   Jan    Red  1  0
3  3   Mar   Blue NA  0
5  5   Mar  Green  0  0
6  6   Jan Purple  1  1

or you can state your columns by indexes:

> monthName  <-  c('Jan','Mar')
> wantedCol  <-  c(1,2,4,5,7) 
> mydata[ mydata$Month %in% monthName,wantedCol]
  ID Month  Color Q1 Q3
1  1   Jan    Red  0 NA
2  2   Jan    Red  1  0
3  3   Mar   Blue NA  0
5  5   Mar  Green  0  0
6  6   Jan Purple  1  1

or if you always want ID,Month and Color :

> monthNum <- 1
> wantedColStat  <-  c('ID','Month','Color')
> wantedColDyna  <-  c('Q1','Q3')
> mydata[ mydata$Month==substring(month.name[monthNum],1,3),match(c(wantedColStat,wantedColDyna),names(mydata))]
  ID Month  Color Q1 Q3
1  1   Jan    Red  0 NA
2  2   Jan    Red  1  0
6  6   Jan Purple  1  1

Upvotes: 1

Related Questions