Hilmar Berger
2010-May-19 16:14 UTC
[R] Strange case of partial matching in .[ - possible bug / wrong documentation?
Hi all, This occurred in R-2.11.0 (WinXP). The R-help page of .[ says that: "Character indices can in some circumstances be partially matched (see pmatch) to the names or dimnames of the object being subsetted (but never for subassignment). Unlike S (Becker et al p. 358)), R has never used partial matching when extracting by [, and as from R 2.7.0 partial matching is not by default used by [[ (see argument exact)." My understanding is therefore that .[ should never try partial matching. However:> df = data.frame(a=c(1,2,3,9), b=c(4,5,6,10)) > rownames(df) = c("ef","gg","hh","fe") > dfa b ef 1 4 gg 2 5 hh 3 6 fe 9 10> df["e",]a b ef 1 4> rownames(df) = c("ef","gg","hh","efg") > df["e",]a b NA NA NA So, it looks like partial matching is done using pmatch("e",rownames(df)) for "[". If this is true, the help page is not correct. Thanks ! Regards, Hilmar --- Hilmar Berger Integromics S.L. / CNB-CSIC Madrid, Spain [[alternative HTML version deleted]]
Duncan Murdoch
2010-May-19 19:54 UTC
[R] Strange case of partial matching in .[ - possible bug / wrong documentation?
On 19/05/2010 12:14 PM, Hilmar Berger wrote:> Hi all, > > This occurred in R-2.11.0 (WinXP). > > The R-help page of .[ says that: > > "Character indices can in some circumstances be partially matched (see > pmatch) to the names or dimnames of the object being subsetted (but never > for subassignment). Unlike S (Becker et al p. 358)), R has never used > partial matching when extracting by [, and as from R 2.7.0 partial matching > is not by default used by [[ (see argument exact)." > > My understanding is therefore that .[ should never try partial matching. >See near the top of the page: "The descriptions here apply only to the default methods." Since indexing is generic, an extraction method can do whatever it wants, and you need to read the particular page to find the behaviour. The page for Extract.data.frame says: "Both |[| and |[[| extraction methods partially match row names. By default neither partially match column names, but |[[| will unless |exact=TRUE|. If you want to do exact matching on row names use |match <http://127.0.0.1:28754/library/base/help/match>| as in the examples." Duncan Murdoch> However: > > > df = data.frame(a=c(1,2,3,9), b=c(4,5,6,10)) > > rownames(df) = c("ef","gg","hh","fe") > > df > a b > ef 1 4 > gg 2 5 > hh 3 6 > fe 9 10 > > > > df["e",] > a b > ef 1 4 > > > rownames(df) = c("ef","gg","hh","efg") > > df["e",] > a b > NA NA NA > > So, it looks like partial matching is done using pmatch("e",rownames(df)) > for "[". If this is true, the help page is not correct. > > Thanks ! > Regards, > Hilmar > > --- > Hilmar Berger > Integromics S.L. / CNB-CSIC > Madrid, Spain > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
David Winsemius
2010-May-19 20:04 UTC
[R] Strange case of partial matching in .[ - possible bug / wrong documentation?
On May 19, 2010, at 12:14 PM, Hilmar Berger wrote:> Hi all, > > This occurred in R-2.11.0 (WinXP). > > The R-help page of .[ says that: > > "Character indices can in some circumstances be partially matched (see > pmatch) to the names or dimnames of the object being subsetted (but > never > for subassignment). Unlike S (Becker et al p. 358)), R has never used > partial matching when extracting by [, and as from R 2.7.0 partial > matching > is not by default used by [[ (see argument exact)." > > My understanding is therefore that .[ should never try partial > matching.That was for [.matrix or similar (as noted at top of the page). You created a different class of variable. ?"[.data.frame "Both [ and [[ extraction methods partially match row names. By default neither partially match column names, but [[ will unless exact=TRUE. If you want to do exact matching on row names use match as in the examples." -- David.> > However: > >> df = data.frame(a=c(1,2,3,9), b=c(4,5,6,10)) >> rownames(df) = c("ef","gg","hh","fe") >> df > a b > ef 1 4 > gg 2 5 > hh 3 6 > fe 9 10 > > >> df["e",] > a b > ef 1 4 > >> rownames(df) = c("ef","gg","hh","efg") >> df["e",] > a b > NA NA NA > > So, it looks like partial matching is done using > pmatch("e",rownames(df)) > for "[". If this is true, the help page is not correct. > > Thanks ! > Regards, > Hilmar > > --- > Hilmar Berger > Integromics S.L. / CNB-CSIC > Madrid, Spain > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.David Winsemius, MD West Hartford, CT
Hilmar Berger
2010-May-19 20:41 UTC
[R] Strange case of partial matching in .[ - possible bug / wrong documentation?
Duncan Murdoch schrieb:> On 19/05/2010 12:14 PM, Hilmar Berger wrote: >> Hi all, >> >> This occurred in R-2.11.0 (WinXP). >> >> The R-help page of .[ says that: >> >> "Character indices can in some circumstances be partially matched (see >> pmatch) to the names or dimnames of the object being subsetted (but >> never >> for subassignment). Unlike S (Becker et al p. 358)), R has never used >> partial matching when extracting by [, and as from R 2.7.0 partial >> matching >> is not by default used by [[ (see argument exact)." >> >> My understanding is therefore that .[ should never try partial matching. >> > > See near the top of the page: "The descriptions here apply only to > the default methods." Since indexing is generic, an extraction method > can do whatever it wants, and you need to read the particular page to > find the behaviour. The page for Extract.data.frame says: > > "Both |[| and |[[| extraction methods partially match row names. By > default neither partially match column names, but |[[| will unless > |exact=TRUE|. If you want to do exact matching on row names use |match > <http://127.0.0.1:28754/library/base/help/match>| as in the examples." > > Duncan MurdochSorry, I should have read the complete help page. Thanks a lot ! Best regards, Hilmar