[R] Reading data from a dataframe
Duncan Murdoch
murdoch at stats.uwo.ca
Sat Feb 9 18:31:19 CET 2008
On 09/02/2008 11:38 AM, David Winsemius wrote:
> "Alexander Ovodenko" <ovodenko at princeton.edu> wrote in
> news:85e440020802090820n281dfffcwffee2807cb483a6e at mail.gmail.com:
>
>> Thanks for the replies to my prior question. My problem is that R
>> always says object not found when I enter a variable name into a
>> command. I converted a Stata file into an Rdata file by first
>> loading the foreign package by entering
>>
>> require(foreign)
>>
>> Then I asked R to read the Stata file by entering
>>
>> pol572a1<- read.dta("C:\\alex\\Graduate Coursework\\Pol
>> 572\\pol572a1.dta")
>
> At this point you should type str(pol572a1) to make sure <everything>
> is the dataframe named pol572a1.
>
>> So now I can do a few things with the data, but I am only able to do
>> so by entering
>>
>> with(pol572a1,
>>
>> before typing another command. So I enter the following for a
>> scatter plot: with(pol572a1, plot(rgnpc, incmean))
>
> If you wanted to avoid the with() function (but why would you?) you
> could have used:
>
> plot(pol572a1$rgnpc, pol572a1$incmean)
>
>> When I run a simple linear regression, I enter the following:
>> with(pol572a1, lm(incmean~rgnpc))
>
> Which creates the model and immediately discards it, because no durable
> object was given the values:
>
>> But when try to get a fitted line, I enter the following:
>> with(pol572a1, lm( share.gnp)<-lm(incmean~rgnpc))
>
> That does not look particular sensible. Assuming that the goal was to
> create a model object named share.gnp, then try instead:
>
> with(pol572a1, share.gnp.mdl <-lm(incmean~rgnpc) )
That won't work: share.gnp.mdl will be created in the local environment
created by with(), and then discarded. You need to put it outside, i.e.
share.gnp.mdl <- with(pol572a1, lm(incmean~rgnpc) )
Duncan Murdoch
>
> (My practice is to label my models, but it is not necessary.)
>
> Then look at share.gnp.mdl with str() and you should see fitted.values.
> You could then plot() the fitted values and the observed values.
>
>
>> But I get the
>> following error message: Error in lm(share.gnp) <- lm(incmean ~
>> rgnpc) : object "share.gnp" not found
>>
>> So what should I do to get R to read the dataframe in the ordinary
>> way with the usual command?
>
> I cannot tell what you mean. It appears you already have the data in a
> dataframe.
>
>> I am willing to scrap this dataframe
>> and use the original Stata file to start over, as long as that
>> resolves the problem.
>>
>> Thanks!
>>
>> Alex
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list