Regression - Make a new group from data

#1
Hi.

I am new to R, and have to do a regression on the dependent variable but only for a certain group which is characterized by one independent variable1 = 1 and another independent variable2 = 2.

So, the regression in question should only be done for the observations which comply with the criteria variable 1=1 and variable 2=2 instead of using all the observations.

The question is: How do I form a group of data which contains only the observations described in the above?
Hope this makes sense.

//Perav
 

Tux

New Member
#2
If I understand correctly what you are saying, you could try something like this:

#Option 1 – create a subset directly into the regression
model ← lm(y ~ x1 + x2, data=mydata, subset=(x1==1 & x2==2))

#Option 2 – create a subset of your data. If your original data=mydata

mydata2 ← mydata[mydata$x1==1 & mydata$x2==2, ]
model2 ← lm(y ~ x1 + x2, data=mydata2)

Is this what you wanted?

Tux
 
#3
Yes, I just created the subset of data, and used it in the regression instead of the original dataset and it worked. Cheers!