title: Linear Regression in R: "Error in eval(expr, envir, enclos) : object not found"
body: <p>I'm trying to do a simple least-squares regression in R and have been getting errors constantly. This is really frustrating, can anyone point out what I am doing wrong?</p>
  
  <p>First I attach the dataset (17 variables, 440 observations, each observation on a single line, no column titles). Here, I get a "masked" error. From what I've read, the "masked" error happens when objects overlap. However here I am not using any packages but the default, and I loaded a new workspace image before this. Not sure what this error refers to?</p>
  
  <pre><code>&gt; cdi=read.table("APPENC02.txt", header=FALSE)
  &gt; attach(cdi)
  The following objects are masked from cdi (position 3):
  
      V1, V10, V11, V12, V13, V14, V15, V16, V17, V2, V3, V4, V5, V6, V7, V8, V9
  </code></pre>
  
  <p>Next, since the data set does not come with headings, I use the <code>colnames()</code> command to add column names, then check my work with the <code>head()</code> command:</p>
  
  <pre><code>colnames(cdi)&lt;- c("IDnmbr","Countynm","Stateabv","LandArea","totpop","youngpct","oldpct","actphy","hspbed","srscrime","hsgrad","BAgrad","povpct","unempct","pcincome","totincome","georegion")
  &gt; head(cdi)
    IDnmbr    Countynm Stateabv LandArea  totpop youngpct oldpct actphy hspbed srscrime hsgrad BAgrad povpct unempct pcincome totincome georegion
  1      1 Los_Angeles       CA     4060 8863164     32.1    9.7  23677  27700   688936   70.0   22.3   11.6     8.0    20786    184230         4
  2      2        Cook       IL      946 5105067     29.2   12.4  15153  21550   436936   73.4   22.8   11    etcetc(manually truncated)
  </code></pre>
  
  <p>Now the most annoying part: I can't get the lm() function to work!</p>
  
  <pre><code>&gt; model1=lm(actphy~totpop)
  Error in eval(expr, envir, enclos) : object 'actphy' not found
  </code></pre>
  
  <p>It's not a upper/lowercase issue, and i've tried <code>"actphy"</code> and <code>actphy</code>. What gives?</p>
  
  <p>Also, the manual i'm following suggests using the <code>attach()</code> function but I've read a few posts discouraging it. What would be a better solution in this case?</p>
  
  <p>Thanks!</p>