[R] indexing by empty string (was RE: Error in predict.randomForest ... subscript out of bounds with NULL name in X)

Liaw, Andy andy_liaw at merck.com
Wed Feb 1 17:36:22 CET 2012


Hi Ista,

When you write a package, you have to anticipate what users will throw at the code.  I can insist that users only input matriices where none of the column names are empty, but that's not what I wish to impose on the users.  I can add the name if it's empty, but as a user I don't want a function to do that, either.  That's why I need to look for a workaround.

Using which() seems rather clumsy for the purpose, as I need to combine those with the non-empty ones, and preserving ordering would be a mess.

Andy 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Ista Zahn
> Sent: Wednesday, February 01, 2012 5:45 AM
> To: r-help at r-project.org
> Subject: Re: [R] indexing by empty string (was RE: Error in 
> predict.randomForest ... subscript out of bounds with NULL name in X)
> 
> Hi Andy,
> 
> On Tuesday, January 31, 2012 08:44:13 AM Liaw, Andy wrote:
> > I'm not exactly sure if this is a problem with indexing by 
> name; i.e., is
> > the following behavior by design?  The problem is that 
> names or dimnames
> > that are empty seem to be treated differently, and one 
> can't index by them:
> > 
> > R> junk = 1:3
> > R> names(junk) = c("a", "b", "")
> > R> junk
> > a b
> > 1 2 3
> > R> junk[""]
> > <NA>
> >   NA
> > R> junk = matrix(1:4, 2, 2)
> > R> colnames(junk) = c("a", "")
> > R> junk[, ""]
> > Error: subscript out of bounds
> 
> You can index them by number, e.g.,
> junk[, 2]
> 
> and you can use which() to find the numbers where the colname 
> is empty.
> 
> junk[, which(colnames(junk) == "")]
> 
> 
> > 
> > I may need to find workaround...
> 
> Going back to the original issue with predict, I don't think 
> you need a 
> workaround. I think you need give your matrix some colnames.
> 
> Best,
> Ista
> 
> > 
> > > -----Original Message-----
> > > From: r-help-bounces at r-project.org
> > > [mailto:r-help-bounces at r-project.org] On Behalf Of 
> Czerminski, Ryszard
> > > Sent: Wednesday, January 25, 2012 10:39 AM
> > > To: r-help at r-project.org
> > > Subject: [R] Error in predict.randomForest ... subscript out
> > > of bounds with NULL name in X
> > > 
> > > RF trains fine with X, but fails on prediction
> > > 
> > > > library(randomForest)
> > > > chirps <-
> > > 
> > > 
> c(20,16.0,19.8,18.4,17.1,15.5,14.7,17.1,15.4,16.2,15,17.2,16,17,14.1)
> > > 
> > > > temp <-
> > > 
> > > c(88.6,71.6,93.3,84.3,80.6,75.2,69.7,82,69.4,83.3,78.6,82.6,80
> > > .6,83.5,76
> > > .3)
> > > 
> > > > X <- cbind(1,chirps)
> > > > rf <- randomForest(X, temp)
> > > > yp <- predict(rf, X)
> > > 
> > > Error in predict.randomForest(rf, X) : subscript out of bounds
> > > 
> > > BTW: Just find out that  apparently predict() does not like
> > > NULL name in
> > > 
> > > X, because this works fine:
> > > > one <- rep(1, length(chirps))
> > > > X <- cbind(one,chirps)
> > > > rf <- randomForest(X, temp)
> > > > yp <- predict(rf, X)
> > > 
> > > Ryszard Czerminski
> > > AstraZeneca Pharmaceuticals LP
> > > 35 Gatehouse Drive
> > > Waltham, MA 02451
> > > USA
> > > 781-839-4304
> > > ryszard.czerminski at astrazeneca.com
> > > 
> > > 
> > > --------------------------------------------------------------
> > > ------------
> > > Confidentiality Notice: This message is private and may
> > > ...{{dropped:11}}
> > > 
> > > ______________________________________________
> > > 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.
> > 
> > Notice:  This e-mail message, together with any 
> attachme...{{dropped:11}}
> > 
> > ______________________________________________
> > 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.
> 
> ______________________________________________
> 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.
> 
Notice:  This e-mail message, together with any attachme...{{dropped:11}}



More information about the R-help mailing list