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
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 foundWorks 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
try:
d[d[,"KB"] == "Eenie",]
or
d[d$KB=="Eenie",]
What you were comparing is just two character strings
("KB"=="Eenie") which
of course is FALSE and won't select anything. You have to explicitly
compare values from the "KB" column of your data
On 1/10/07, Mark Hempelmann <neo27@rakers.de>
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
> @
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
[[alternative HTML version deleted]]