Dear all This may be obvious, but I cannot get it working. I'm trying to subset & sort a data frame in one go. x <- iris x$Species1 <- as.character(x$Species) ##subsetting alone works fine with(x, x[Sepal.Length==6.7,]) ##sorting alone works fine with(x, x[order(Sepal.Length, rev(sort(Species1))),]) ##gets subsetted, but not sorted as expected with(x, x[(Sepal.Length==6.7) & order(Sepal.Length, rev(sort(Species1))),]) ##gets subsetted, but sorts very strangely xa <- with(x, x[Sepal.Length==6.7,]); with(xa, xa[order(Sepal.Length, rev(sort(Species1))),]) xa <- with(x, x[Sepal.Length==6.7,]); with(xa, xa[order(rev(sort(Species1))),]) I've checked The R Inferno, Quick-R and several other places with no obvious solution. Any ideas? Regards Liviu -- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
On Sun, Mar 6, 2011 at 11:49 PM, Liviu Andronic <landronimirc at gmail.com> wrote:> Dear all > This may be obvious, but I cannot get it working. I'm trying to subset > & sort a data frame in one go. > x <- iris > x$Species1 <- as.character(x$Species) > ##subsetting alone works fine > with(x, x[Sepal.Length==6.7,]) > ##sorting alone works fine > with(x, x[order(Sepal.Length, rev(sort(Species1))),]) > ##gets subsetted, but not sorted as expected > with(x, x[(Sepal.Length==6.7) & order(Sepal.Length, rev(sort(Species1))),]) > ##gets subsetted, but sorts very strangely > xa <- with(x, x[Sepal.Length==6.7,]); with(xa, xa[order(Sepal.Length, > rev(sort(Species1))),]) > xa <- with(x, x[Sepal.Length==6.7,]); with(xa, xa[order(rev(sort(Species1))),]) >And of course I found the culprit after sending the e-mail: wrong call order. The following does what I want, although it's a bit messy: xa <- with(x, x[Sepal.Length==6.7,]); with(xa, xa[rev(order(sort(Species1))),]) xa <- subset(x, Sepal.Length==6.7); with(xa, xa[rev(order((sort(Species1)))),]) Regards Liviu> I've checked The R Inferno, Quick-R and several other places with no > obvious solution. > > Any ideas? Regards > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail >-- Do you know how to read? http://www.alienetworks.com/srtest.cfm http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader Do you know how to write? http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
On Mar 6, 2011, at 5:49 PM, Liviu Andronic wrote:> Dear all > This may be obvious, but I cannot get it working. I'm trying to subset > & sort a data frame in one go. > x <- iris > x$Species1 <- as.character(x$Species) > ##subsetting alone works fine > with(x, x[Sepal.Length==6.7,]) > ##sorting alone works fine > with(x, x[order(Sepal.Length, rev(sort(Species1))),]) > ##gets subsetted, but not sorted as expected > with(x, x[(Sepal.Length==6.7) & order(Sepal.Length, > rev(sort(Species1))),])You are doing logical operations between a logical and numeric vector. You should get a bunch of TRUE's and FALSE's and it might be as you say, strange. (The ordering will not occur.)> ##gets subsetted, but sorts very strangely > xa <- with(x, x[Sepal.Length==6.7,]); with(xa, xa[order(Sepal.Length, > rev(sort(Species1))),]) > xa <- with(x, x[Sepal.Length==6.7,]); with(xa, > xa[order(rev(sort(Species1))),]) > > I've checked The R Inferno, Quick-R and several other places with no > obvious solution. > > Any ideas? Regards > Liviu > > --David Winsemius, MD Heritage Laboratories West Hartford, CT
Hi: One approach is through the data.table package: library(data.table) df <- as.data.frame(data.table(iris, key = 'Species')[Sepal.Length == 6.7]) str(df) Using Species as a key variable automatically sorts by Species; the bracketing allows you to subset on Sepal.Length == 6.7. ddply() in the plyr package also works: library(plyr) ddply(iris, .(Species), subset, Sepal.Length == 6.7) HTH, Dennis On Sun, Mar 6, 2011 at 2:49 PM, Liviu Andronic <landronimirc@gmail.com>wrote:> Dear all > This may be obvious, but I cannot get it working. I'm trying to subset > & sort a data frame in one go. > x <- iris > x$Species1 <- as.character(x$Species) > ##subsetting alone works fine > with(x, x[Sepal.Length==6.7,]) > ##sorting alone works fine > with(x, x[order(Sepal.Length, rev(sort(Species1))),]) > ##gets subsetted, but not sorted as expected > with(x, x[(Sepal.Length==6.7) & order(Sepal.Length, rev(sort(Species1))),]) > ##gets subsetted, but sorts very strangely > xa <- with(x, x[Sepal.Length==6.7,]); with(xa, xa[order(Sepal.Length, > rev(sort(Species1))),]) > xa <- with(x, x[Sepal.Length==6.7,]); with(xa, > xa[order(rev(sort(Species1))),]) > > I've checked The R Inferno, Quick-R and several other places with no > obvious solution. > > Any ideas? Regards > Liviu > > > -- > Do you know how to read? > http://www.alienetworks.com/srtest.cfm > http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader > Do you know how to write? > http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail > > ______________________________________________ > R-help@r-project.org 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. >[[alternative HTML version deleted]]
On Mon, Mar 7, 2011 at 1:38 AM, David Winsemius <dwinsemius at comcast.net> wrote:> subset(x[order(x$Species1), ], ?Sepal.Length==6.7 ) >Thank you all for the suggestions. Now I can do exactly what I wanted. Regards Liviu