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

Ista Zahn istazahn at gmail.com
Wed Feb 1 11:45:26 CET 2012


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.



More information about the R-help mailing list