0

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!

UseR10085
  • 7,120
  • 3
  • 24
  • 54
badenduser
  • 43
  • 1
  • 6

2 Answers2

1

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.

neilfws
  • 32,751
  • 5
  • 50
  • 63
  • Thank you! Also, I realized I made a new matrix when I changed some variables into log form and essentially was calling from two different matrices. – badenduser Feb 28 '17 at 03:53
0

@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.

acylam
  • 18,231
  • 5
  • 36
  • 45