[R] select subsets in data frame

From: Mark Hempelmann <neo27_at_rakers.de>
Date: Wed 10 Jan 2007 - 11:10:05 GMT


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



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:18:35 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 - 13: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.