Re: [R] select subsets in data frame

From: Chuck Cleland <ccleland_at_optonline.net>
Date: Wed 10 Jan 2007 - 11:27:19 GMT

Mark Hempelmann wrote:
> Dear WizaRds!
>
> A trivial question indeed on selecting subsets in data frames. I am
> sorry. Unfortunately, I did not find any helpful information on the
> introduction, searched the help archive and read in introductory books.
> Please help:
>
> I want to select column "KB" which is read via read.csv2 as a data.frame
> into d. I checked that it is indeed a data.frame object and included the
> correct header information in line 1. For example purposes, look at this
> small object:
> <<*>>= (4)
> d <- data.frame(A=1:3, Date=c("01.01.07","02.01.07","03.01.07"),
> KB=c("Eenie", "Meenie", "Miney") )
>
> d["KB"=="Eenie",] # gives
> @
> output-start
> [1] A Date KB
> <0 rows> (or 0-length row.names)
> output-end
> @

  Try this instead:

subset(d, KB == "Eenie")

  A Date KB
1 1 01.01.07 Eenie

?subset

> If I follow Venables/ Ripley in Modern Applied Statistics with S, it
> should look like this:
>
> <<*>>= (5)
> library(MASS)
> attach(painters)
> painters[Colour>=17,]
> @
> gives the correct subset. But
> d[KB=="Eenie",] # gives
>
> Error in `[.data.frame`(d, KB == "Eenie", ) :
> object "KB" not found

  Works for me if I attach the data frame first:

attach(d)

d[KB == "Eenie",]

  A Date KB
1 1 01.01.07 Eenie

> I need every KB named Eenie. What did I do wrong? The alternative I
> found seems to be quite complicated:
>
> <<*>>= (6)
> d[which( d[,"KB"]=="Eenie" ), ]
> @
> output-start
> A Date KB
> 1 1 01.01.07 Eenie
> output-end
>
> Thank you so much for your help.
>
> cheers
> mark
>
>
> "I believe I found the missing link between animal and civilized man.
> It's us." -- Konrad Lorenz
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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.

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

______________________________________________
R-help@stat.math.ethz.ch 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.
Received on Wed Jan 10 22:33:12 2007

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 Wed 10 Jan 2007 - 12:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.