Dear All, I have a fraction of 60./(220.*6.), which equals 1./22. My question is how to cancel the fraction so I actually get 1/22 (actually I just need the factor 22) using R. I tried it with prime factor decomposition from the package "schoolmath":> require("schoolmath")Loading required package: schoolmath> nom <- prime.factor(60) > nom[1] 2 2 3 5> denom <- prime.factor(220 * 6) > denom[1] 2 2 2 3 5 11 So far so good, now I just need to take all elements which are in nom out of denom (if possible). For this I use the package "sets", its function set_complement should exactly do that, but:> require(sets) > set_complement(nom, denom){2, 2, 11} It gives me {2,2,11} and 2 * 2 * 11 is 44 and not 22. What am I doing wrong? Can anybody help me? Is there maybe a more elegant way to cancel a fraction than by prime factor decomposition? Many thanks in advance, Hannes -- View this message in context: http://r.789695.n4.nabble.com/cancelling-in-fraction-tp2062218p2062218.html Sent from the R help mailing list archive at Nabble.com.
Here is a different approach that may work for you, or give you a starting place:> library(MASS) > fractions(60/(220*6))[1] 1/22 Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of capybara! > Sent: Friday, April 23, 2010 7:49 AM > To: r-help at r-project.org > Subject: [R] cancelling in fraction > > > Dear All, > > I have a fraction of 60./(220.*6.), which equals 1./22. > My question is how to cancel the fraction so I actually get 1/22 > (actually I > just need the factor 22) using R. I tried it with prime factor > decomposition > from the package "schoolmath": > > > require("schoolmath") > Loading required package: schoolmath > > nom <- prime.factor(60) > > nom > [1] 2 2 3 5 > > denom <- prime.factor(220 * 6) > > denom > [1] 2 2 2 3 5 11 > > So far so good, now I just need to take all elements which are in nom > out of > denom (if possible). > For this I use the package "sets", its function set_complement should > exactly do that, but: > > > require(sets) > > set_complement(nom, denom) > {2, 2, 11} > > It gives me {2,2,11} and 2 * 2 * 11 is 44 and not 22. > What am I doing wrong? Can anybody help me? > Is there maybe a more elegant way to cancel a fraction than by prime > factor > decomposition? > > Many thanks in advance, > > Hannes > > > -- > View this message in context: http://r.789695.n4.nabble.com/cancelling- > in-fraction-tp2062218p2062218.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Greg has provided a solution. Just to answer the question of why set_complement() is not doing what you think it should: You need to change your *vectors* nom and denom to *sets* with as.set(). -Peter Ehlers On 2010-04-23 9:42, Greg Snow wrote:> Here is a different approach that may work for you, or give you a starting place: > >> > library(MASS) >> > fractions(60/(220*6)) > [1] 1/22>> > From:r-help-bounces at r-project.org [mailto:r-help-bounces at r- >> > project.org] On Behalf Of capybara! >> > Sent: Friday, April 23, 2010 7:49 AM >> > To:r-help at r-project.org >> > Subject: [R] cancelling in fraction >> > >> > >> > Dear All, >> > >> > I have a fraction of 60./(220.*6.), which equals 1./22. >> > My question is how to cancel the fraction so I actually get 1/22 >> > (actually I >> > just need the factor 22) using R. I tried it with prime factor >> > decomposition >> > from the package "schoolmath": >> > >>> > > require("schoolmath") >> > Loading required package: schoolmath >>> > > nom<- prime.factor(60) >>> > > nom >> > [1] 2 2 3 5 >>> > > denom<- prime.factor(220 * 6) >>> > > denom >> > [1] 2 2 2 3 5 11 >> > >> > So far so good, now I just need to take all elements which are in nom >> > out of >> > denom (if possible). >> > For this I use the package "sets", its function set_complement should >> > exactly do that, but: >> > >>> > > require(sets) >>> > > set_complement(nom, denom) >> > {2, 2, 11} >> > >> > It gives me {2,2,11} and 2 * 2 * 11 is 44 and not 22. >> > What am I doing wrong? Can anybody help me? >> > Is there maybe a more elegant way to cancel a fraction than by prime >> > factor >> > decomposition? >> > >> > Many thanks in advance, >> > >> > Hannes >> > >> > >> > -- >> > View this message in context:http://r.789695.n4.nabble.com/cancelling- >> > in-fraction-tp2062218p2062218.html >> > Sent from the R help mailing list archive at Nabble.com. >> > >> > ______________________________________________ >> > R-help at r-project.org mailing list >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guidehttp://www.R-project.org/posting- >> > guide.html >> > and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Peter Ehlers University of Calgary
Thanks a lot! I had not heard about the fractions() function before, it is very useful!! Cheers! -- View this message in context: http://r.789695.n4.nabble.com/cancelling-in-fraction-tp2062218p2062475.html Sent from the R help mailing list archive at Nabble.com.