ana kozomara
2003-Apr-20 19:01 UTC
[R] how to use apply with a function created by ourselfs...?
Hi. I'm a real newbie in R, so I don't know how to apply, a function created by myself... prediction<-function(a,b) { .. } to a vector... It doesn't seem to understand lapply(vector, prediction())... Anyone can help me? Thanks in advance, ana
Spencer Graves
2003-Apr-20 20:05 UTC
[R] how to use apply with a function created by ourselfs...?
By default, arithmetic in R is vectorized, with a scalar being a vector of length 1. Consider the following example: prediction <- function(a, b) { a+b } > prediction(1:3, 4:6) [1] 5 7 9 hope this helps, spencer graves ana kozomara wrote:> Hi. > I'm a real newbie in R, so > I don't know how to apply, a function created by myself... > > prediction<-function(a,b) > { > .. > } > > to a vector... > It doesn't seem to understand > lapply(vector, prediction())... > Anyone can help me? > Thanks in advance, > ana > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
ANA KOZOMARA
2003-Apr-21 08:26 UTC
[R] how to use apply with a function created by ourselfs...?
..hey thanks for the answer, both of you... but :-(, I don't seem to have an answer to my problem... I tried with what u suggested, but it doesn't work yet... so I will precise it... I have a data frame... and I would like to apply the function "prediction" which acts on two vectors prediction(linear,ind), to my data frame,I mean to all the rows of my data frame... something like a function "Map" in Mathematica... Shortly, I want to apply function "prediction" to a list of arguments, not to a single argument... I hope now it is more clear... Thanks for the answers once more, best regards, ana ______________________________________________________________________ Original poruka Od: Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> Za: Spencer Graves <spencer.graves at pdf.com> Tema: Re: [R] how to use apply with a function created by ourselfs...? Primljena: 20 Apr 2003 22:31:52 Spencer Graves <spencer.graves at pdf.com> writes:> By default, arithmetic in R is vectorized, with a scalar being a > vector of length 1. > > Consider the following example: > > prediction <- function(a, b) > { > a+b > } > > prediction(1:3, 4:6) > [1] 5 7 9 > > hope this helps, spencer gravesNot sure that was the question... If something else was meant, the answer might be lapply(v,prediction,b=5) or do.call("prediction",as.list(v)) Rephrasing the question might help... -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
ANA KOZOMARA
2003-Apr-21 09:43 UTC
[R] how to use apply with a function created by ourselfs...?
hey thanks a lot... finally I decided to write my own fonction which will do it... I was completely lost with rbind, mode="list"... I find slightely weird the R-kind-of-thinking...:))) Anyway, i'm trying to acomplish my project in R and not c spirit, so i'll try what u suggests, ..hey, thanks a lot again... ______________________________________________________________________ Original poruka Od: Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk> Za: ANA KOZOMARA <magnolia at absolutok.net> Tema: Re: [R] how to use apply with a function created by ourselfs...? Primljena: 21 Apr 2003 11:27:48 "ANA KOZOMARA" <magnolia at absolutok.net> writes:> ..hey thanks for the answer, both of you... > but :-(, I don't seem to have an answer to my problem... > I tried with what u suggested, but it doesn't work yet... > so I will precise it... > I have a data frame... > and I would like to apply the function "prediction" which acts on two > vectors > prediction(linear,ind), > to my data frame,I mean to all the rows of my data frame... > something like a function "Map" in Mathematica... > Shortly, I want to apply function "prediction" to a list of arguments,not> to a single argument... > I hope now it is more clear... > Thanks for the answers once more, > best regards, > anaIt depends on your prediction function. The best thing is to arrange that it vectorizes over its arguments, as Spencer Graves suggested. Then it's just prediction(df[,1],df[,2]) or, if you want something that works for more than two rows, do.call("prediction", df) if your prediction() does not vectorize (i.e. a and b has to be scalars), you can do two things: pr.row <- function(row) do.call("prediction",row) apply(df, 1, pr.row) or create a vectorized prediction(), as in pr.vec <- function(a,b) sapply(seq(along=a),function(i)prediction(a[i],b[i])) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907