[R] subsetting data set dimenion problem

Chuck Cleland ccleland at optonline.net
Sun Feb 27 15:35:10 CET 2005


Clint Harshaw wrote:
> (See DAAG book, p. 173, ex. 3)
> 
> I'm a new user of R, and I'm following the DAAG text. I want to create a 
> subset of the races2000 data frame, but get errors because of a mismatch 
> of values in some columns:
> 
>  > library(DAAG)
>  > attach(races2000)
>  > hills2000 <- races2000[races2000$type == 'hill']
> Error in as.matrix.data.frame(x) : dim<- : dims [product 770] do not 
> match the length of object [771]
> 
> However, if I follow the solution given, and remove redundant columns 1 
> through 6 and column 11 (which I won't need, since I know they are going 
> to have the same value), I don't get the error:
> 
>  > hills2000 <- races2000[races2000$type == 'hill', -c(1:6,11)]
>  > hills2000
>                        dist climb      time      timef
> Tiso Carnethy          6.00  2500 0.7822222  0.9191667
> [...]
> Cornalees              5.50   800 0.6183333         NA
> [...]
> 
> What is causing the error with my original subsetting? I speculated it 
> was related to the NA values, but there is an NA in the resulting 
> hills2000, corresponding to the Cornalees hill race.

   The error in the first subsetting is caused by a missing comma.  Try 
this:

races2000[races2000$type == 'hill',]

> Thanks,
> Clint

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 452-1424 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list