Re: [R] Listing Variables

From: Gabor Grothendieck <>
Date: Thu 04 May 2006 - 01:04:31 EST

Column names in iris that contain the string Sepal:

grep("Sepal", names(iris), value = TRUE)

On 5/3/06, Farrel Buchinsky <> wrote:
> How does one create a vector whose contents is the list of variables in a
> dataframe pertaining to a particular pattern?
> This is so simple but I cannot find a straightforward answer.
> I want to be able to pass the contents of that list to a "for" loop.
> So let us assume that one has a dataframe whose name is Data. And let us
> assume one had the height of a group of people measured at various ages.
> It could be made up of vectors Data$PersonalID, Data$FirstName,
> Data$LastName, Data$Height.1, Data$Height.5, Data$Height.9,
> Data$Height.10,Data$Height.12,Data$Height.20....many many more variables.
> How would one create a vector of all the Height variable names.
> The simple workaround is to not bother creating the vector "Data$Height.1"
> "Data$Height.5" "Data$Height.9" "Data$Height.10"
> "Data$Height.12""Data$Height.20"...but rather just to use the sapply
> function. However with some functions the sapply will not work and it is
> necessary to supply each variable name to a function (see thread at
> Repeating tdt function on thousands of variables)
> This is such a core capability. I would like to see it in the R-Wiki but
> could not find it there.
> --
> Farrel Buchinsky, MD
> Pediatric Otolaryngologist
> Allegheny General Hospital
> Pittsburgh, PA
> ______________________________________________
> mailing list
> PLEASE do read the posting guide!
> mailing list PLEASE do read the posting guide! Received on Thu May 04 04:47:48 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 04 May 2006 - 06:10:05 EST.

Mailing list information is available at Please read the posting guide before posting to the list.