[R] Question on apply() with more information...
Petr Pikal
petr.pikal at precheza.cz
Tue Sep 19 09:55:15 CEST 2006
Hi
both lapply and mapply can give you what you want but you have to
select only desired part.
e.g.
spec.apply<- function(sl, x) {
d<-dim(x)[1]
d2<-dim(x)[2]*2*d
vec<-seq(d,d2,2)
Z<-as.numeric(mapply(predict,sl, x))[vec]
dim(Z) <-c(d,length(vec)/d)
Z
}
but it is probably slower than simple for loop. However maybe some
clever use of do.call can help.
Best regards
Petr
On 18 Sep 2006 at 13:26, Gunther Höning wrote:
From: Gunther Höning <gunther.hoening at ukmainz.de>
To: "'Petr Pikal'" <petr.pikal at precheza.cz>
Date sent: Mon, 18 Sep 2006 13:26:25 +0200
Copies to: r-help at stat.math.ethz.ch
Subject: Re: [R] Question on apply() with more information...
> Hi,
>
> I tried both ideas, but it isn't that what I'm looking for.
> I want to avoid for loop, because the matrix is of big size(1200*1200
> entries)
>
> With a loop I would do:
>
> for ( i in seq(along = SmoothList))
> {
> Xarry[i,] <- predict(SmoothList[[i]],Xarry[i,])$y
> }
>
> Actually I want to do more than just to predict a value, but it isn't
> important for the initial question...
>
> Gunther
>
> -----Ursprüngliche Nachricht-----
> Von: Petr Pikal [mailto:petr.pikal at precheza.cz]
> Gesendet: Montag, 18. September 2006 11:44
> An: Gunther Höning
> Cc: r-help at stat.math.ethz.ch
> Betreff: Re: AW: [R] Question on apply() with more information...
>
> Hi
>
> If I am correct apply do not choose from SmoothList as you expected.
> Instead probably
>
> lapply(SmoothList, predict,Xarray)
> or
> mapply(predict,SmoothList, Xarray)
>
> can give you probably what you want.
>
> HTH
> Petr
>
>
> On 18 Sep 2006 at 9:26, Gunther Höning wrote:
>
> From: Gunther Höning <gunther.hoening at ukmainz.de>
> To: "'Petr Pikal'" <petr.pikal at precheza.cz>,
> <r-help at stat.math.ethz.ch>
> Subject: AW: [R] Question on apply() with more information...
> Date sent: Mon, 18 Sep 2006 09:26:01 +0200
>
> > Ok.
> > I tried this too, but it still doesn't work.
> > Here some more information to try out, but just an excerpt of Xarray
> >
> > x <- c(0.11,0.25,0.45,0.65,0.80,0.95,1)
> > Y <-
> > matrix(c(15,83,57,111,150,168,175,37,207,142,277,375,420,437),nrow=2
> > )
> >
> > sm <- function(y,x){smooth.spline(x,y)} SmoothList <-
> > apply(Y,1,sm,x) NewValues <- function(x,LIST){predict(LIST,x)}
> > Xarray <-
> > matrix(c(0.15,0.56,0.66,0.45,0.19,0.17,0.99,0.56,0.77,0.41,0.11,0.63
> > ,0 .42,0. 43),nrow=2)
> >
> >
> > apply(Xarray, 2, NewValues,SmoothList) apply(Xarray, 2,
> > NewValues,LIST=SmoothList)
> >
> >
> >
> > -----Ursprüngliche Nachricht-----
> > Von: Petr Pikal [mailto:petr.pikal at precheza.cz]
> > Gesendet: Montag, 18. September 2006 08:43
> > An: Gunther Höning; r-help at stat.math.ethz.ch
> > Betreff: Re: [R] Question on apply()
> >
> > Hi
> >
> > not much information about what can be wrong. As nobody knows your
> > Xarray and SmoothList it is hard to guess. You even omitted to show
> > what "does not work" So here are few guesses.
> >
> > predict usually expects comparable data apply(Xarray, 2,
> > NewValues,LIST=SmoothList)
> >
> >
> > HTH
> > Petr
> >
> >
> >
> >
> > On 18 Sep 2006 at 8:05, Gunther Höning wrote:
> >
> > From: Gunther Höning <gunther.hoening at ukmainz.de>
> > To: <r-help at stat.math.ethz.ch>
> > Date sent: Mon, 18 Sep 2006 08:05:28 +0200
> > Subject: [R] Question on apply()
> >
> > > Dear list,
> > >
> > > I try to do the following:
> > > I have an list of length n, with elements done by smooth.spline
> > > (SmoothList). Now I have a matrix with n rows and m columns with
> > > x-values(Xarray) Now I want ot predict the y-values. Therefor I
> > > want to take the first element of SmoothList and the first row of
> > > Xarray and predict for each element in Xarray the y value. And
> > > then take the second element of SmoothList and second row of
> > > Xarray, third row of SmoothList and third row of Xarray and so
> > > on....
> > >
> > > I tried following:
> > >
> > > NewValues <- function(x,LIST){predict(LIST,x)} apply(Xarray, 2,
> > > NewValues,SmoothList)
> > >
> > > But it don't work.
> > >
> > > Could anybody help please ?
> > >
> > > Gunther
> > >
> > > ______________________________________________
> > > 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 and provide commented,
> > > minimal, self-contained, reproducible code.
> >
> > Petr Pikal
> > petr.pikal at precheza.cz
> >
>
> Petr Pikal
> petr.pikal at precheza.cz
>
> ______________________________________________
> 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 and provide commented,
> minimal, self-contained, reproducible code.
Petr Pikal
petr.pikal at precheza.cz
More information about the R-help
mailing list