[R] lines.formula with subset

Steven McKinney smckinney at bccrc.ca
Fri Nov 14 05:41:48 CET 2008




 


> -----Original Message-----
> From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org]
> On Behalf Of John Field
> Sent: Thursday, November 13, 2008 4:41 PM
> To: r-help at r-project.org
> Subject: [R] lines.formula with subset
> 
> Dear list,
> 
> When I try to use lines.formula with subset and another argument I
> get an error.
> e.g.
> 
> x<-1:5
> y<-c(1,3,NA,2,5)
> plot(y~x, type="n")  # set up frame
> lines(y~x, subset=!is.na(y))  # works OK
> lines(y~x, type="o", col="blue")  # works OK
> # but
> lines(y~x, subset=!is.na(y), col="red")  # gives an error:
> 
> Error in if (length(x) == l) x[s] else x : argument is of length zero
> 
> Why does this happen?
> 

It happens because the function
graphics:::lines.formula

tries to assess the number of rows of data in the data frame
containing the variables in the formula y~x
(see the line of code 
l <- nrow(data)
in graphics:::lines.formula
This is the 'el' in the 'length(x) == l'
portion of the line you see in the error message)

Because you did not provide the data frame,
nrow(data) returns NULL, and thus the
if() clause is 'length(x) == NULL' which
yields answer logical(0), an invalid
answer in an if() clause.

Done this way, all is well:

mydf <- data.frame(x = 1:5, y = c(1,3,NA,2,5))
plot(y~x, type="n", data = mydf)  # set up frame
lines(y~x, subset=!is.na(y), data = mydf)  # works OK
lines(y~x, type="o", col="blue", data = mydf)  # works OK
lines(y~x, subset=!is.na(y), col="red", data = mydf)  # works OK

The formula - based functions expect to see a dataframe object
from the 'data' arg, but don't enforce this in this case. 

This may qualify as a logical bug in the graphics:::lines.formula
function.  An error should have been thrown before the if()
clause evaluation, but I'm not sure where in the chain of
function calls the check for a valid data object should be 
done and the error thrown. Otherwise, the data objects
y and x that you set up should have been passed downwards
in some fashion for evaluation.  R-core members who know 
the rules better than I will have to determine how best to 
handle this one.

HTH 

Steven McKinney

Statistician
Molecular Oncology and Breast Cancer Program
British Columbia Cancer Research Centre

email: smckinney at bccrc.ca
tel: 604-675-8000 x7561

BCCRC
Molecular Oncology
675 West 10th Ave, Floor 4
Vancouver B.C. 
V5Z 1L3

Canada



> With thanks,
> John
> 
> =========================
> John Field Consulting Pty Ltd
> 10 High Street, Burnside SA 5066
> Phone 08 8332 5294 or 0409 097 586
> Fax   08 8332 1229
> Email  JohnField at ozemail.com.au
> 
> ______________________________________________
> 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