[R] select subsets in data frame
Chuck Cleland
ccleland at optonline.net
Wed Jan 10 12:27:19 CET 2007
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 at 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
More information about the R-help
mailing list