Re: [R] Re ading Functions that are in a Vector
Hi, You said, sumstats - c(mean,sd) sumstats[1] #Gives this error but this is not an error! You created a list that contains two functions, and sumstats[1] simply prints the first one. HTH, baptiste __ 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.
Re: [R] Re ading Functions that are in a Vector
Also, have a look at each() in the plyr package, library(plyr) each(length, mean, var)(rnorm(100)) baptiste 2009/9/28 trumpetsaz stephaniezim...@gmail.com: I am trying to write a function that will have an input of a vector of functions. Here is a simplistic example. sumstats - c(mean,sd) sumstats[1] #Gives this error # sumstats[1] #[[1]] #function (x, ...) #UseMethod(mean) #environment: namespace:base I thought about restricting the input to character variables such as the following sumstats2 - c(mean,sd) Is there a way to change mean to the function mean? -- View this message in context: http://www.nabble.com/Reading-Functions-that-are-in-a-Vector-tp25639720p25639720.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.
[R] Re ading Functions that are in a Vector
I am trying to write a function that will have an input of a vector of functions. Here is a simplistic example. sumstats - c(mean,sd) sumstats[1] #Gives this error # sumstats[1] #[[1]] #function (x, ...) #UseMethod(mean) #environment: namespace:base I thought about restricting the input to character variables such as the following sumstats2 - c(mean,sd) Is there a way to change mean to the function mean? -- View this message in context: http://www.nabble.com/Reading-Functions-that-are-in-a-Vector-tp25639720p25639720.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Re ading Functions that are in a Vector
On Sun, Sep 27, 2009 at 10:36 PM, trumpetsaz stephaniezim...@gmail.com wrote: I am trying to write a function that will have an input of a vector of functions. Here is a simplistic example. sumstats - c(mean,sd) sumstats[1] #Gives this error # sumstats[1] #[[1]] #function (x, ...) #UseMethod(mean) #environment: namespace:base That's not an error. The code asked it to return the first function so that's what it did. I thought about restricting the input to character variables such as the following sumstats2 - c(mean,sd) Is there a way to change mean to the function mean? This gives a vector of functions given a vector of their names: sumstat1 - lapply(sumstats2, get) __ 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.
Re: [R] Re ading Functions that are in a Vector
On 28/09/2009, at 3:36 PM, trumpetsaz wrote: I am trying to write a function that will have an input of a vector of functions. Here is a simplistic example. sumstats - c(mean,sd) sumstats[1] #Gives this error # sumstats[1] #[[1]] #function (x, ...) #UseMethod(mean) #environment: namespace:base I thought about restricting the input to character variables such as the following sumstats2 - c(mean,sd) Is there a way to change mean to the function mean? You *can't* have a vector of functions, as far I know. The entries of a vector must be (numeric, character, or logical) scalars. You *can* have a *list* of functions; this might be the way you want to go. To ``change mean to the function mean'' --- set ?get. cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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.
Re: [R] Re ading Functions that are in a Vector
On Sep 27, 2009, at 11:07 PM, Rolf Turner wrote: On 28/09/2009, at 3:36 PM, trumpetsaz wrote: I am trying to write a function that will have an input of a vector of functions. Here is a simplistic example. sumstats - c(mean,sd) sumstats[1] #Gives this error # sumstats[1] #[[1]] #function (x, ...) #UseMethod(mean) #environment: namespace:base I thought about restricting the input to character variables such as the following sumstats2 - c(mean,sd) Is there a way to change mean to the function mean? You *can't* have a vector of functions, as far I know. The entries of a vector must be (numeric, character, or logical) scalars. You *can* have a *list* of functions; this might be the way you want to go. To ``change mean to the function mean'' --- set ?get. That is prezactly what he created: umstats - c(mean,sd) str(umstats) List of 2 $ :function (x, ...) $ :function (x, na.rm = FALSE) And what's more it works if it is accessed with [[: umstats[[1]](c(1,2,3,4,5)) [1] 3 David Winsemius, MD Heritage Laboratories West Hartford, CT __ 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.
Re: [R] Re ading Functions that are in a Vector
On 28/09/2009, at 4:30 PM, David Winsemius wrote: On Sep 27, 2009, at 11:07 PM, Rolf Turner wrote: On 28/09/2009, at 3:36 PM, trumpetsaz wrote: I am trying to write a function that will have an input of a vector of functions. Here is a simplistic example. sumstats - c(mean,sd) sumstats[1] #Gives this error # sumstats[1] #[[1]] #function (x, ...) #UseMethod(mean) #environment: namespace:base I thought about restricting the input to character variables such as the following sumstats2 - c(mean,sd) Is there a way to change mean to the function mean? You *can't* have a vector of functions, as far I know. The entries of a vector must be (numeric, character, or logical) scalars. You *can* have a *list* of functions; this might be the way you want to go. To ``change mean to the function mean'' --- set ?get. That is prezactly what he created: umstats - c(mean,sd) str(umstats) List of 2 $ :function (x, ...) $ :function (x, na.rm = FALSE) And what's more it works if it is accessed with [[: umstats[[1]](c(1,2,3,4,5)) [1] 3 Never ceases to amaze me how R can get it right in settings and with syntaxes that you would think can't ***possibly*** work! cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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.