Dear all I think my problem is not complicated but I'm having difficulties to solve it. v is a vector: v=c(p1 , p2 , p3 , p4), and f is a function: f : v -> w , where w=c(p1 , p2*(1-p1) , p3*(1-p2)*(1-p1) , p4*(1-p3)*(1-p2)*(1-p1)) I write the function f as: f<- function(w,x,y,z) {c(w,x*(1-w),y*(1-x)*(1-w),z*(1-y)*(1-x)*(1-w))} f(a,b,c,d) it works well. But now I want to apply f to each row of a data frame with 4 columns: d<-data.frame(a=seq(1,10), b=seq(11,20), c=seq(21,30),d=seq(31,40)) t(apply(d,1,f)) is not working? I think each element of each row is not corresponding to w,x,y,z and now I'm lost? Can someone help me? thks J. Silva [[alternative HTML version deleted]]
jose silva wrote:>Dear all > > > >Its very difficult to read your post due to all the blank lines, so I cannot really read it very well, but what about writing your function as (not tried) fun <- function(x) c(x[1], .... or if that doesn't work look into ?mapply But you should really look into the settings of your mail program! Kjetil> > > > > > > >I think my problem is not complicated but I'm having difficulties to solve it. > > > > >v is a vector: v=c(p1 , p2 , p3 , p4), and f is a function: f : v -> w , where > > > > >w=c(p1 , p2*(1-p1) , p3*(1-p2)*(1-p1) , p4*(1-p3)*(1-p2)*(1-p1)) > > > > > > > > > >I write the function f as: > > > > >f<- function(w,x,y,z) {c(w,x*(1-w),y*(1-x)*(1-w),z*(1-y)*(1-x)*(1-w))} > > > > >f(a,b,c,d) it works well. > > > > > > > > > >But now I want to apply f to each row of a data frame with 4 columns: > > > > >d<-data.frame(a=seq(1,10), b=seq(11,20), c=seq(21,30),d=seq(31,40)) > > > > >t(apply(d,1,f)) is not working? > > > > >I think each element of each row is not corresponding to w,x,y,z and now I'm lost? > > > > > > > > > >Can someone help me? > > > > > > > > > >thks > > > > > > > > > >J. Silva > [[alternative HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > > >-- Kjetil Halvorsen. Peace is the most effective weapon of mass construction. -- Mahdi Elmandjra -- No virus found in this outgoing message. Checked by AVG Anti-Virus.
On 6/5/05, jose silva <chess.player at oninet.pt> wrote:> Dear all > > > > > > > > > > I think my problem is not complicated but I'm having difficulties to solve it. > v is a vector: v=c(p1 , p2 , p3 , p4), and f is a function: f : v -> w , where > w=c(p1 , p2*(1-p1) , p3*(1-p2)*(1-p1) , p4*(1-p3)*(1-p2)*(1-p1)) > > I write the function f as: > f<- function(w,x,y,z) {c(w,x*(1-w),y*(1-x)*(1-w),z*(1-y)*(1-x)*(1-w))} > f(a,b,c,d) it works well.> But now I want to apply f to each row of a data frame with 4 columns: > d<-data.frame(a=seq(1,10), b=seq(11,20), c=seq(21,30),d=seq(31,40)) > t(apply(d,1,f)) is not working? > I think each element of each row is not corresponding to w,x,y,z and now I'm lost? > Can someone help me?apply passes each of row of d to f as a single vector, not 4 individual elements, so in terms of your f define fv which is like f but takes a vector argument: fv <- function(x) f(x[1], x[2], x[3], x[4]) apply(d,1,fv) # or maybe you want t(apply(d,1,fv)) # In fact, since your f works with vectors as arguments this would do: as.data.frame(fv(d))
Kjetil and Gabor: thank you both so much for for yor help silva <chess.player@oninet.pt> wrote:> Dear all > >> I think my problem is not complicated but I'm having difficulties to solve it. > v is a vector: v=c(p1 , p2 , p3 , p4), and f is a function: f : v -> w , where > w=c(p1 , p2*(1-p1) , p3*(1-p2)*(1-p1) , p4*(1-p3)*(1-p2)*(1-p1)) > > I write the function f as: > f<- function(w,x,y,z) {c(w,x*(1-w),y*(1-x)*(1-w),z*(1-y)*(1-x)*(1-w))} > f(a,b,c,d) it works well.> But now I want to apply f to each row of a data frame with 4 columns: > d<-data.frame(a=seq(1,10), b=seq(11,20), c=seq(21,30),d=seq(31,40)) > t(apply(d,1,f)) is not working? > I think each element of each row is not corresponding to w,x,y,z and now I'm lost? > Can someone help me?apply passes each of row of d to f as a single vector, not 4 individual elements, so in terms of your f define fv which is like f but takes a vector argument: fv <- function(x) f(x[1], x[2], x[3], x[4]) apply(d,1,fv) # or maybe you want t(apply(d,1,fv)) # In fact, since your f works with vectors as arguments this would do: as.data.frame(fv(d)) [[alternative HTML version deleted]]