[R] question about user written function (newb question)
Quick newb question about R relating to the line of code below: rawCool = read.zoo(cooling.txt, FUN = as.chron, format = %m/%d/%Y %H:%M, sep = \t, aggregate = function(x) tail(x, 1)) I'm wondering what the specifics are for the argument where it has aggregate = function(x) tail(x, 1). I understand that it removes the last row of duplicates/aggregates in the zoo series. I'm confused as to why tail(x, 1), a built in function in the utils package, requires the coder to treat it as a user written function thus defining the assignment, in this case an argument, with function(x). Why can't the coder just write tail(x, 1) instead? Also, with the argument 'x', within tail, I'm assuming it's looking at all columns simultaneously within the zoo series? Is that correct to say? Thanks. -- View this message in context: http://r.789695.n4.nabble.com/question-about-user-written-function-newb-question-tp4422187p4422187.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] question about user written function (newb question)
Short answer to a very good question: one has to use function(x) tail(x, 1) syntax to avoid using the default tail(x, 6). There are some other ways to achieve the same thing, but I think this syntax is generally preferred for its clarity. Other question: yes I believe so. Michael On Sun, Feb 26, 2012 at 9:06 AM, knavero knav...@gmail.com wrote: Quick newb question about R relating to the line of code below: rawCool = read.zoo(cooling.txt, FUN = as.chron, format = %m/%d/%Y %H:%M, sep = \t, aggregate = function(x) tail(x, 1)) I'm wondering what the specifics are for the argument where it has aggregate = function(x) tail(x, 1). I understand that it removes the last row of duplicates/aggregates in the zoo series. I'm confused as to why tail(x, 1), a built in function in the utils package, requires the coder to treat it as a user written function thus defining the assignment, in this case an argument, with function(x). Why can't the coder just write tail(x, 1) instead? Also, with the argument 'x', within tail, I'm assuming it's looking at all columns simultaneously within the zoo series? Is that correct to say? Thanks. -- View this message in context: http://r.789695.n4.nabble.com/question-about-user-written-function-newb-question-tp4422187p4422187.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.
Re: [R] question about user written function (newb question)
On Sun, Feb 26, 2012 at 9:06 AM, knavero knav...@gmail.com wrote: Quick newb question about R relating to the line of code below: rawCool = read.zoo(cooling.txt, FUN = as.chron, format = %m/%d/%Y %H:%M, sep = \t, aggregate = function(x) tail(x, 1)) I'm wondering what the specifics are for the argument where it has aggregate = function(x) tail(x, 1). I understand that it removes the last row of duplicates/aggregates in the zoo series. I'm confused as to why tail(x, 1), a built in function in the utils package, requires the coder to treat it as a user written function thus defining the assignment, in this case an argument, with function(x). Why can't the coder just write tail(x, 1) instead? Also, with the argument 'x', within tail, I'm assuming it's looking at all columns simultaneously within the zoo series? Is that correct to say? Thanks. 1. tail(x, 1) is not a function. Its the operation of executing the function tail with the values x and 1. Even if x existed it would be an error since aggregate= requires a function, not a value. One could have written aggregate = tail except that the default is 6 elements whereas we want 1. One could write this where tail1 is defined to be the same as tail except its hard coded to use the last element of x only: tail1 - function(x) tail(x, 1) read.zoo(...whatever..., aggregate = tail1) 2. aggregate= ultimately calls aggregate in the core of R and that works by looking at the columns one by one. For a particular column it splits it into groups (in the case of aggregate.zoo the groups are defined by rows having the same times) and then for each group applying the indicated function. See ?read.zoo and the vignette(zoo-read) for more. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.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.