Skip to content

How does the data.frame function generate column names?

2 messages · H Roark, Joshua Wiley

#
Hi,

Welcome to R!  What you have run into is a feature of how subsetting
works.  By default, it converts to the lowest possible dimensions.
The odd name you see, "d.8.10...c..",  is an attempt to convert "
d[8:10, "c"]  " into a valid name.  R does this approximately by
converting disallowed characters (like ":") into periods (.).  This is
because data.frame() uses whatever was passed to it as the name of the
column, unless whatever it is already has a column name.  Here is some
code (you should be able to copy and paste), with comments that
explains a bit further and hopefully gives you a better feel for
indexing and creating data frame objects.

Cheers,

Josh

################################################
## your data (in one step)
d <- data.frame(a = 1:10, b = 11:20, c = 21:30)

## because only one column of 'd' is selected, the conversion
## to lowest possible dimensions is 1 (a vector)
## and that loses its column name, so use drop = FALSE
f <- data.frame(d[8:10, "c", drop = FALSE])

## another option is to explicitly name the column
g <- data.frame(c = d[8:10, "c"])

## here you have selected two columns so there must
## be at least two dimensions, and names are kept
g2 <-data.frame(d[8:10, c("b", "c")])

## to "see" what is happening
d[8:10, "c", drop = FALSE]
d[8:10, "c", drop = TRUE] # default

## for more details, see the documentation
?"["  # see the "drop" argument description
?data.frame # under the "value" section on names

################################################
On Sun, Jan 23, 2011 at 1:53 PM, H Roark <hrbuilder at hotmail.com> wrote: