Hi All
Slightly lost on how should I pass values to a function I am calling
using apply.
apply( veh_drg_animal1[ , c("readCount","gene_length")] ,1, stats() )
here stats is a custom function where I want to pass two parameters
from data frame as shown and a third argument which is constant per
instance of apply.
stats <- function ( arg1,agr2,agr3) {
## computation using three parameters
}
Passing values to a function when using apply
9 messages · Henrique Dallazuanna, Erik Iverson, Abhishek Pratap
This is not a reproducible example. You might simply want:
apply(veh_drg_animal1[, c("readCount", "gene_length")], 1, stats, arg2,
arg3)
But your "two parameters" from the data.frame are really going to be
passed as one vector, and then within the stats function you can access
them individually. I'm guessing you really want a 2-parameter stats
function call, but it's hard to tell without a real example.
Abhishek Pratap wrote:
Hi All
Slightly lost on how should I pass values to a function I am calling
using apply.
apply( veh_drg_animal1[ , c("readCount","gene_length")] ,1, stats() )
here stats is a custom function where I want to pass two parameters
from data frame as shown and a third argument which is constant per
instance of apply.
stats <- function ( arg1,agr2,agr3) {
## computation using three parameters
}
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100427/d60580e9/attachment.pl>
Hi Henrique and Erik
I still get a error. See below.
apply(veh_drg_animal1[ ,c("readCount","gene_length")] ,1, stats,
total=55000000)
Error in FUN(newX[, i], ...) :
element 1 is empty;
the part of the args list of '(' being evaluated was:
(length_gene)
stats<- function(count,length_gene,total) {
( count/( total * (length_gene ) ) ) * ( 10^9)
}
Thanks!
-Abhi
On Tue, Apr 27, 2010 at 2:18 PM, Henrique Dallazuanna <wwwhsd at gmail.com> wrote:
Try this:
apply( veh_drg_animal1[ , c("readCount","gene_length")] ,1, ?stats, agr3 =
your_constant)
On Tue, Apr 27, 2010 at 3:10 PM, Abhishek Pratap <abhishek.vit at gmail.com>
wrote:
Hi All
Slightly lost on how should I pass values to a function I am calling
using apply.
apply( veh_drg_animal1[ , c("readCount","gene_length")] ,1, ?stats() )
here stats is a custom function where I want to pass two parameters
from data frame as shown and a third argument which is constant per
instance of apply.
stats <- function ( arg1,agr2,agr3) {
## computation using three parameters
}
______________________________________________ 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.
-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100427/5655cc5f/attachment.pl>
Henrique Dallazuanna wrote:
try this:
apply(veh_drg_animal1[ ,c("readCount","gene_length")] ,1,
function(x)stats(x[1], x[2], total=55000000))
I agree with this, that was my point in my original reply. Apply is *not* passing 2 arguments simply because you are selecting two columns of the input object, it's passing *one argument*, which is the *entire row* of the input object. You can then access them individually within the stats function, as Henrique shows.
Hi Guys Thank you for clearing something I dint know. Just wondering the reason of putting the word function(x) in the apply function when we have already declared stats function separately. I better understand how the arguments are passed. Thanks! -Abhi
On Tue, Apr 27, 2010 at 2:46 PM, Erik Iverson <eriki at ccbr.umn.edu> wrote:
Henrique Dallazuanna wrote:
try this:
apply(veh_drg_animal1[ ,c("readCount","gene_length")] ,1,
function(x)stats(x[1], x[2], total=55000000))
I agree with this, that was my point in my original reply. ?Apply is *not* passing 2 arguments simply because you are selecting two columns of the input object, it's passing *one argument*, which is the *entire row* of the input object. ?You can then access them individually within the stats function, as Henrique shows.
Abhishek Pratap wrote:
Hi Guys Thank you for clearing something I dint know. Just wondering the reason of putting the word function(x) in the apply function when we have already declared stats function separately. I better understand how the arguments are passed. Thanks! -Abhi
That's creating a *new* function, one without a name, that basically "breaks up" the x argument into two values, and passes those along with the third argument and passes these to your stats function. You either have to write stats to accept two arguments, or leave stats accepting three arguments and use this method.
Makes sense. Thanks guys for your quick reverts! -Abhi
On Tue, Apr 27, 2010 at 3:34 PM, Erik Iverson <eriki at ccbr.umn.edu> wrote:
Abhishek Pratap wrote:
Hi Guys Thank you for clearing something I dint know. Just wondering the reason of putting the word function(x) ?in the apply function when we have already declared stats function separately. I better understand how the arguments are passed. Thanks! -Abhi
That's creating a *new* function, one without a name, that basically "breaks up" the x argument into two values, and passes those along with the third argument and passes these to your stats function. ?You either have to write stats to accept two arguments, or leave stats accepting three arguments and use this method.