[R] select subsets in data frame

Mark Hempelmann
Wed, 10 Jan 2007 11:10:05 -0000

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
@
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

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