[R] Problem with apply list to function: numerical expression has 4 elements: only the first used

Kehl Dániel kehld at ktk.pte.hu
Sun May 24 09:22:03 CEST 2015


Dear Narua,

Others might suggest other things but here are some of my points.
In general it is not a good idea to call a function F as it is an abbreviation for FALSE.
Also it might be a good idea to write your functions in a way that they check the length of the arguments and behave accordingly  (if nothing else, with loops). It is really nice to take a look at the family of apply functions (although I also have problems with them sometimes).

I did not check, but you can try something like this

ff <- function(m,k)((1/(sum(sqrt(1:m))))*sqrt(k))
FF <- function(m,i)(sum(ff(m,1:i)))
x <- 1:4

sapply(x, ff, m=4)
sapply(x, FF, m=4)


Keep up with using R! :)

Best,
kd
________________________________________
Feladó: R-help [r-help-bounces at r-project.org] ; meghatalmazó: Narua [maria.mi8 at gmx.de]
Küldve: 2015. május 24. 0:21
To: r-help at r-project.org
Tárgy: [R] Problem with apply list to function: numerical expression has 4 elements: only the first used

Hello ,

I want to try R for statistics.

Therefore, I defined a function f with parameters m and k, which calculates
a to sqrt(x) proportional density function:
f <- function(m,k)((1/(sum(sqrt(1:m))))*sqrt(k))

A function F sums the results in order to get a distribution function:
F <- function(m,i)(sum(f(m,1:i)))
It works e.g. for m=4:
> F(4,1)
[1] 0.1627005
> F(4,4)
[1] 1

If I want to see all results for m=4, I write
> F(4,1:4)
[1] 0.1627005
Warning message:
In 1:i : numerical expression has 4 elements: only the first used

what does the error message mean?
and how can I solve it?

I've already tried rapply but I get the same error and googled for a while
but there is nowhere a satisfying answer

Can someone help me here?

many thanks in advance,
Narua




--
View this message in context: http://r.789695.n4.nabble.com/Problem-with-apply-list-to-function-numerical-expression-has-4-elements-only-the-first-used-tp4707588.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list