Hi My code looks like this I have two parameters x and par1. X contains values and par1 contains the function which i required to use if par1 is max then output should be max(x). FUN <- match.fun(par1) result=FUN(x) Is it possible to incorporate the unique count of x within this code eg x=("a","b","a","c") . The output should be 3 ----- Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/Help-in-using-unique-count-by-match-function-tp4569859p4569859.html Sent from the R help mailing list archive at Nabble.com.
Hi Your question is rather cryptic. Why the output shall be 3? What has unique count to do with match function? Maybe you want something what is described in switch help. See ?switch Regards Petr> > Hi > > My code looks like this > > I have two parameters x and par1. X contains values and par1 containsthe> function which i required to use > > if par1 is max then output should be max(x). > > FUN <- match.fun(par1) > result=FUN(x) > > Is it possible to incorporate the unique count of x within this code > > eg > > x=("a","b","a","c") . The output should be 3 > > ----- > Thanks in Advance > Arun > -- > View this message in context:http://r.789695.n4.nabble.com/Help-in-using-> unique-count-by-match-function-tp4569859p4569859.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. Michael Weylandt <michael.weylandt@gmail.com>
2012-Apr-19 14:21 UTC
[R] Help in using unique count by match function
I think the OP is looking for the construct length(unique(x)) but not really sure what the rest of the question is. Michael On Apr 19, 2012, at 2:12 AM, Petr PIKAL <petr.pikal at precheza.cz> wrote:> Hi > > Your question is rather cryptic. Why the output shall be 3? What has > unique count to do with match function? > > Maybe you want something what is described in switch help. > > See > > ?switch > > Regards > Petr > >> >> Hi >> >> My code looks like this >> >> I have two parameters x and par1. X contains values and par1 contains > the >> function which i required to use >> >> if par1 is max then output should be max(x). >> >> FUN <- match.fun(par1) >> result=FUN(x) >> >> Is it possible to incorporate the unique count of x within this code >> >> eg >> >> x=("a","b","a","c") . The output should be 3 >> >> ----- >> Thanks in Advance >> Arun >> -- >> View this message in context: > http://r.789695.n4.nabble.com/Help-in-using- >> unique-count-by-match-function-tp4569859p4569859.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. > > ______________________________________________ > 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.
Hello, arunkumar1111 wrote> > Hi > > My code looks like this > > I have two parameters x and par1. X contains values and par1 contains the > function which i required to use > > if par1 is max then output should be max(x). > > FUN <- match.fun(par1) > result=FUN(x) > > Is it possible to incorporate the unique count of x within this code > > eg > > x=("a","b","a","c") . The output should be 3 >Yes, it's possible. See if this is what you want. fun <- function(x, par1){ FUN <- match.fun(par1) result <- FUN(x) result } x <- c("a","b","a","c") fun(x, max) # using an unnamed function, 'y' is just a place holder fun(x, function(y) length(unique(y))) # using a named function count.uniq <- function(y) length(unique(y)) fun(x, count.uniq) Hope this helps, Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Help-in-using-unique-count-by-match-function-tp4569859p4571812.html Sent from the R help mailing list archive at Nabble.com.