[R] Row-wise recurive function call

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Tue Nov 9 08:52:01 CET 2010


try this:

apply(data.matrix(a), 1, playFn)


I hope it helps.

Best,
Dimitris


On 11/9/2010 8:32 AM, Santosh Srinivas wrote:
> Dear Group,
>
> I have a following dataset:
>> a
>      A  B  C  D
> 1  22  3 31 40
> 2  26 31 36 32
> 3   3  7 49 16
> 4  24 40 27 26
> 5  20 45 47  0
> 6  34 43 11 18
> 7  48 48 24  2
> 8   3 16 39 48
> 9  20 49  7 21
> 10 17 36 47 10
>
>> dput(a)
> structure(list(A = c(22L, 26L, 3L, 24L, 20L, 34L, 48L, 3L, 20L,
> 17L), B = c(3L, 31L, 7L, 40L, 45L, 43L, 48L, 16L, 49L, 36L),
>      C = c(31L, 36L, 49L, 27L, 47L, 11L, 24L, 39L, 7L, 47L), D = c(40L,
>      32L, 16L, 26L, 0L, 18L, 2L, 48L, 21L, 10L)), .Names = c("A",
> "B", "C", "D"), class = "data.frame", row.names = c(NA, -10L))
>
>
> I have a function that works off EVERY individual ROW to throw a result.
> Like
>
> playFn<- function (x){
> + result = ((x$A+6*x$B)/(3*x$C)+20)*x$D
> + return(result)
> + }
>
>
> I want to apply the function for every row .... can I use an "apply"
> function ... tried but not been able to ...
> e.g. print(rapply(a,playFn))
>
> Please advise.
>
> Thanks,
> S
>
> ______________________________________________
> 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.
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list