Bert Gunter
2018-Oct-22 16:02 UTC
[R] match() question or needle haystack problem for a data.frame
I suggest you spend a bit of time with an R tutorial or two and, in particular learn about "logical indexing," as this basic R construct seems to be mysterious to you. Cheers, Bert On Mon, Oct 22, 2018 at 8:22 AM Knut Krueger <rhelp at krueger-family.de> wrote:> Am 22.10.18 um 17:01 schrieb Eric Berger: > > v <- match(Mydata$DATA1, needles, nomatch=NA) > > > found <- Mydata[ !is.na <http://is.na>(v), ] > > > missing <- Mdata[ is.na <http://is.na>(v), ] > > Thank you it is working, additionally as Bert suggested, it seems that > > Mydata[Mydata$DATA1 %in% needles,] > > is doing the same. > > Kind Regards Knut > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]
Knut Krueger
2018-Oct-22 17:33 UTC
[R] match() question or needle haystack problem for a data.frame
Am 22.10.18 um 18:02 schrieb Bert Gunter:> I suggest you spend a bit of time with an R tutorial or two and, in > particular learn about "logical indexing," as this basic R construct > seems to be mysterious to you.Hi Bert, especially the "match" help area is a little bit confusing. And additional using a couple of programming languages with different logical operators and different syntax is misleading me sometimes to the wrong direction... So the logic operations itself are not mysterious ;-) but the translation to R. And thank's a lot to give me the keyword to find for what I am was looking for: logical indexing. I can not count the sites where I was looking to solve my problem .. with the wrong keywords ... :-) Kind Regards Knut
Knut Krueger
2018-Oct-25 14:06 UTC
[R] "logical indexing, " [was] match() question or needle haystack problem for a data.frame
Hi Bert, another question for indexing 1. is there a good manual for indexing 2. is it possible to get !%in% f.e x =data.frame("A"=c(1:5),"C"=c("A","B","C","D","E")) y =data.frame("B"=c(1,3,5)) test = x[x$A %in% y$B,] test = x[x$A %in% y$B,] test A C 1 1 A 3 3 C 5 5 E means: the result where I am looking for is: test A C 2 2 B 4 4 D no problem with a loop but in a short way I do not find a solution Kind Regards Knut
peter dalgaard
2018-Oct-25 14:13 UTC
[R] "logical indexing, " [was] match() question or needle haystack problem for a data.frame
> On 25 Oct 2018, at 16:06 , Knut Krueger <rhelp at krueger-family.de> wrote: > > Hi Bert, > another question for indexing > 1. is there a good manual for indexing?Extract is at least authoritative....> > 2. is it possible to get !%in% > f.eYes: x[!(x$A %in% y$B),] -pd> x =data.frame("A"=c(1:5),"C"=c("A","B","C","D","E")) > y =data.frame("B"=c(1,3,5)) > test = x[x$A %in% y$B,] > test = x[x$A %in% y$B,] > > test > A C > 1 1 A > 3 3 C > 5 5 E > > > means: the result where I am looking for is: > test > A C > 2 2 B > 4 4 D > > no problem with a loop but in a short way I do not find a solution > > Kind Regards Knut > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Rui Barradas
2018-Oct-25 14:17 UTC
[R] "logical indexing, " [was] match() question or needle haystack problem for a data.frame
Hello, 2. x[!x$A %in% y$B, ] 1. An Introduction to R, file R-into.pdf in your docs folder. http://www.cookbook-r.com/Basics/Indexing_into_a_data_structure/ Hope this helps, Rui Barradas ?s 15:06 de 25/10/2018, Knut Krueger escreveu:> Hi Bert, > another question for indexing > 1. is there a good manual? for indexing > > 2.? is it possible to get !%in% > f.e > x =data.frame("A"=c(1:5),"C"=c("A","B","C","D","E")) > y =data.frame("B"=c(1,3,5)) > test = x[x$A %in% y$B,] > test = x[x$A %in% y$B,] > > test > A C > 1 1 A > 3 3 C > 5 5 E > > > means: the result where I am looking for is: > test > A C > 2 2 B > 4 4 D > > no problem with a loop but in a short way I do not find a solution > > Kind Regards Knut > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Fox, John
2018-Oct-26 12:25 UTC
[R] "logical indexing, " [was] match() question or needle haystack problem for a data.frame
Dear Knut,> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Knut > Krueger > Sent: Friday, October 26, 2018 2:29 AM > To: r-help mailing list <r-help at r-project.org> > Subject: Re: [R] "logical indexing, " [was] match() question or needle haystack > problem for a data.frame > > Am 25.10.18 um 16:13 schrieb peter dalgaard: > > > > > > Yes: x[!(x$A %in% y$B),] > > Ok thats in my opinion a little workaroundNot a work-around but a solution. That is, one can't expect to find an existing function for every problem.> why?: > > There is an > = and !Actually == and !> < and > > > > means the opposite is available between terms. > > why is there f.e no %!in%, %notin% or !%in% > > This would be more intuitive.If you feel strongly about this, it's not hard to supply it:> `%!in%` <- function(x, y) !(x %in% y) > (1:4) %in% (2*1:4)[1] FALSE TRUE FALSE TRUE> (1:4) %!in% (2*1:4)[1] TRUE FALSE TRUE FALSE Best, John ----------------------------------------------------------------- John Fox Professor Emeritus McMaster University Hamilton, Ontario, Canada Web: https://socialsciences.mcmaster.ca/jfox/> > Kind regards Knut > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.