badenduser
badenduser

Reputation: 43

R Conditional Regression with Multiple Conditions

I am trying to run a regression in R based on two conditions. My data has binary variables for both year and another classification. I can get the regression to run properly while only using 1 condition:

# now time for the millions of OLS
# format: OLSABCD where ABCD are binary for the values of MSA/UA and years
# A = 1 if MSA, 0 if UA
# B = 1 if 2010
# C = 1 if 2000
# D = 1 if 1990

OLS1000<-summary(lm(lnrank ~ lnpop, data = subset(df, msa==1)))
OLS1000

However I cannot figure out how to get both the MSA/UA classification to work with the year variables as well. I have tried:

OLS1100<-summary(lm(lnrank ~ lnpop, data = subset(df, msa==1, df$2010==1)))
OLS1100

But it returns the error:

Error: unexpected numeric constant in "OLS1100<-summary(lm(lnrank ~ lnpop,
data = subset(df, msa==1, df$2010"

How can I get the program to run utilizing both conditions?

Thank you again!

Upvotes: 0

Views: 3047

Answers (2)

acylam
acylam

Reputation: 18661

@neilfws pointed out the "numeric as column names issue", but there is actually another issue in your code.

The third argument of subset() is actually reserved for the select =, which lets you choose which columns to include (or exclude). So the correct syntax should be:

subset(df, msa == 1 & `2010` == 1)

instead of

subset(df, msa == 1, `2010` == 1)

This second code would not give you an error, but it also would not give you the right condition.

Upvotes: 0

neilfws
neilfws

Reputation: 33772

The problem is:

df$2010

If your data really has a column named 2010, then you need backticks around it:

df$`2010`

And in your subset, don't specify df twice:

subset(df, msa == 1, `2010` == 1)

In general it's better if column names don't start with digits. It's also best not to name data frames df, since that's a function name.

Upvotes: 1

Related Questions