[R] dataframe subsetting behaviour

Douglas Grove dgrove at fhcrc.org
Wed Jan 22 23:06:16 CET 2003


I'm trying to understand a behaviour that I have encountered
and can't fathom.

Here's some code I will use to illustrate the behaviour:

# start with some data frame "a" having some named columns
a <- data.frame(a=rep(1,3),c=rep(2,3),d=rep(3,3),e=rep(4,3))

# create a subset of the original data frame, but include a
# name "b" that is not present in my original data frame
b <- a[,c("a","b","c")]

## Up until now no errors are issued, but the following commands
## will give the error shown:

b[1,]     ## "Error in x[[j]] : subscript out of bounds"
b[1,2]    ## "Error in "names<-.default"(*tmp*, value = cols) : 
          ##  names attribute must be the same length as the vector"

Can anyone explain to me the meaning of these error messages in terms
of R is actually doing?  These error messages had me baffled and 
it took me hours to track down that the source of the error was an 
incorrect column name in my data frame subsetting.

Doug Grove
Statistical Research Associate
Fred Hutchinson Cancer Research Center
Seattle, WA

More information about the R-help mailing list