[R] Unable to Plot using headers.

Jonathan Daily biomathjdaily at gmail.com
Fri May 27 21:25:48 CEST 2011


I would caution against using attach(), however, if you are not in an
interactive session. In functions and scripts, errors can often cause
the interpreter to exit before the detach(), leaving your data on the
search path. 99% of all attach/detach cases can be handled by ?with
and ?within. The issue with attach can be seen in this example:

dat <- data.frame(a = 1, b = 2)

test <- function(x){
attach(dat)
if(x) stop("STOP")
print(a)
print(b)
detach(dat)
}

a
test(F)
a

a
test(T)
a


On Fri, May 27, 2011 at 3:13 PM, stephen's mailinglist account
<stephen4mailinglists at googlemail.com> wrote:
>>> My data is a 2 column 8000 row table (saved as .txt file) imported into
>>> R.
>>>
>>>>demand=read.delim("C:\\Documents and Settings\\E066582\\My
>>> Documents\\R\\R-2.13.0\\bin\\demand.txt")
>>>
>>> First rows with headers are as shown:
>>>
>>>> demand[1,]
>>>
>>>        Date Qty
>>>
>>> 1 12/31/2006   1
>>>
>>> With two columns Date/Qty.
>>>
>>> I attempted to plot and received an error. My headers were not found as
>>> an object in R...maybe I'm missing something, but I was under the
>>> impression I didn't need to create each header as an object...
>>>
>>>> plot(Qty, Date)
>>>
>>> Error in plot(Qty, Date) : object 'Qty' not found
>>>
>>>> plot(Date, Qty)
>>>
>>> Error in plot(Date, Qty) : object 'Date' not found
>
>> You need to tell R where to find them, since they aren't R objects.
>> (You can see all the actual objects with ls().)
>>
>> Two of the many options:
>>
>> with(demand, plot(Qty, Date))
>>
>> plot(demand$Qty, demand$Date)
>
>
> does it help to tell R that there is a header row in the file?
> demand<-read.delim("path\\demand.txt", header=TRUE)
>
> if you want to address Qty and Date directly you could attach (other
> options were given above)
> attach(demand)
> then
> plot(Qty, Date)
> should work
> --
> Stephen
>
> ______________________________________________
> 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.
>



-- 
===============================================
Jon Daily
Technician
===============================================
#!/usr/bin/env outside
# It's great, trust me.



More information about the R-help mailing list