[R] converting a for loop into a foreach loop
jim holtman
jholtman at gmail.com
Mon Jan 23 14:47:22 CET 2012
If 'idh' is the set of indices resulting from the 'split', then your
function would probably have to look like this:
myfun <- function(x){
H.scv <- Hscv(data[x, ], pilot = "unconstr")
KDE <- kde(data[x, ], H=H.scv, approx.cont=TRUE)
}
This is based on your original example where 'data' was the object
name of your data. You want to use the indices to access the relavent
rows in your data. Not sure what your function is supposed to be
returning; right now it return the last value which would be the
result of the call to 'kde'. If you want both results returned, then
the last statement should possibly be:
list(H.scv, KDE)
which will return the values in a list that you can then extract from.
On Mon, Jan 23, 2012 at 1:14 AM, kalee <kathryn.lee1 at students.mq.edu.au> wrote:
> Thanks Jim for your reply. I've split the dataset into indices but am now
> having problem with my function. The function I need to use is in two parts:
>
>> H.scv <- Hscv(idh, pilot = "unconstr") ## commands from the ks package
>>
>> KDE <- kde(idh, H=H.scv, approx.cont=TRUE)
>
> I've tried to write these as a function separately to be applied within the
> sapply():
>
> myfun <- function(x){
> H.scv <- Hscv(idh, pilot = "unconstr")
> KDE <- kde(idh, H=H.scv, approx.cont=TRUE)
> }
>
> HR <- sapply(idh, myfun)
>
> However, the first part of my function (H.scv) doesn't recognise the idh
> (when applied within the sappily or on its own). Is there anyway to write
> the code so that it would recognise each indices? I.e ID = 1, day =1, hour
> =0 so indices number is 1.1.0 and then H.scv would be Hscv(1.1.0, pilot =
> "unconstr")?
>
> Basically I need to be able to use the Hscv and kde test for each hour of
> each day for each ID that I have and can't get the tests (functions) to
> recognise the input at the moment (Hscv input must be a vector or matrix).
>
> Thanks,
> Kate
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/converting-a-for-loop-into-a-foreach-loop-tp4309646p4319971.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list