[R] Row-wise recurive function call

Santosh Srinivas santosh.srinivas at gmail.com
Tue Nov 9 08:32:49 CET 2010


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



More information about the R-help mailing list