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.
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
Dear kd,
thanks for your fast reply :-D
hm, I thougth, F is normally the abbrevation, but in R FALSE represents
"false"?
Anyway, your function works.
I had actually a little more complicate functions (put I posted in the forum
first an easier example):
f<-function(a,b)((1/(sum(sqrt(1:a))))*sqrt(b))
F<-function(a,b)(sum(f(a,1:b)))
F(5,3)
[1] 0.4946433
F(5,5)
[1] 1
therefore, 0.4946433 is the distribution of b=3 with a=5 Elements.
FInv<-function(s,a,b) if(isTRUE(all.equal(F(a,b),s, tolerance=0.00001)))
cat("F(",s,",",a,",",b,") equals s; ") else cat("F(",a,",",b,") does not
equal to s; ")
FInv is the reverse function. you put a probability in the function and
recieve the distribution b of a special a.
I wanted to have a function FInv' to pass on s for all a and all b to
FInv(s,a,b) in order to find out, which probability fits to which a and b.
And I couln't pass on a and b so easily, I got the error: "In 1:i :
numerical expression has 4 elements: only the first used "
I think, the reason is, that R expects a number, but receives a list, and
that produces the error.
I tried
x<-c(1:10)
FooInv<-function(s) foreach(i=1:10)%do%sapply(x,FInv,s=s,k=i) and that
works:
FooInv(0.4946433)
F(s = 0.4946433 ,m = 5 ,k = 3 ) equals s; [[1]]
Foreach is a for-loop, and sapply matches FInv for every element of the list
x.
But now another problem occured:
Obviously R gets anywhere a NULL-pointer, because the full output is:
F(s = 0.4946433 ,m = 5 ,k = 3 ) equals s; [[1]]
[[1]][[1]]
NULL
[[1]][[2]]
NULL
[[1]][[3]]
NULL
[[1]][[4]]
NULL
[[1]][[5]]
NULL
...
in every loop.
Do you know how I can get rid of the NULL-outputs?
if this problem is solved, I have all I need in order to calculate the
distribution and its inverse function :-)
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-tp4707588p4707611.html
Sent from the R help mailing list archive at Nabble.com.
Dear kd,
thanks for your fast reply
hm, I thougth, F is normally the abbrevation, but in R FALSE represents
"false"?
Anyway, your function works and I got the idea to work with "apply"
I had actually a little more complicate functions (put I posted in the forum
first an easier example):
f<-function(a,b)((1/(sum(sqrt(1:a))))*sqrt(b))
F<-function(a,b)(sum(f(a,1:b)))
F(5,3)
[1] 0.4946433
F(5,5)
[1] 1
therefore, 0.4946433 is the distribution of b=3 with a=5 Elements.
FInv<-function(s,a,b) if(isTRUE(all.equal(F(a,b),s, tolerance=0.00001)))
cat("F(",s,",",a,",",b,") equals s; ") else cat("F(",a,",",b,") does not
equal to s; ")
FInv is the reverse function. you put a probability in the function and
recieve the distribution b of a special a.
I wanted to have a function FInv' to pass on s for all a and all b to
FInv(s,a,b) in order to find out, which probability fits to which a and b.
And I couln't pass on a and b so easily, I got the error: "In 1:i :
numerical expression has 4 elements: only the first used "
I think, the reason is, that R expects a number, but receives a list, and
that produces the error.
I tried
x<-c(1:10)
FooInv<-function(s) foreach(i=1:10)%do%sapply(x,FInv,s=s,k=i) and that
works:
FooInv(0.4946433)
F(s = 0.4946433 ,m = 5 ,k = 3 ) equals s; [[1]]
Foreach is a for-loop, and sapply matches FInv for every element of the list
x.
But now another problem occured:
Obviously R gets anywhere a NULL-pointer, because the full output is:
F(s = 0.4946433 ,m = 5 ,k = 3 ) equals s; [[1]]
[[1]][[1]]
NULL
[[1]][[2]]
NULL
[[1]][[3]]
NULL
[[1]][[4]]
NULL
[[1]][[5]]
NULL
...
in every loop.
Do you or someone else know how I can get rid of the NULL-outputs?
if this problem is solved, I have all I need in order to calculate the
distribution and its inverse function :-)
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-tp4707588p4707612.html
Sent from the R help mailing list archive at Nabble.com.
On May 24, 2015, at 6:35 AM, Narua <maria.mi8 at gmx.de> wrote:
Dear kd,
thanks for your fast reply
hm, I thougth, F is normally the abbrevation, but in R FALSE represents
"false"?
Anyway, your function works and I got the idea to work with "apply"
I had actually a little more complicate functions (put I posted in the forum
first an easier example):
f<-function(a,b)((1/(sum(sqrt(1:a))))*sqrt(b))
F<-function(a,b)(sum(f(a,1:b)))
F(5,3)
[1] 0.4946433
F(5,5)
[1] 1
therefore, 0.4946433 is the distribution of b=3 with a=5 Elements.
FInv<-function(s,a,b) if(isTRUE(all.equal(F(a,b),s, tolerance=0.00001)))
cat("F(",s,",",a,",",b,") equals s; ") else cat("F(",a,",",b,") does not
equal to s; ")
FInv is the reverse function. you put a probability in the function and
recieve the distribution b of a special a.
I wanted to have a function FInv' to pass on s for all a and all b to
FInv(s,a,b) in order to find out, which probability fits to which a and b.
And I couln't pass on a and b so easily, I got the error: "In 1:i :
numerical expression has 4 elements: only the first used "
I think, the reason is, that R expects a number, but receives a list, and
that produces the error.
No. First off, it?s not an error but rather a warning. Here?s the easiest way of seeng what produces it>
m=1:4
1:m
[1] 1
Warning message:
In 1:m : numerical expression has 4 elements: only the first used
? David.
I tried
x<-c(1:10)
FooInv<-function(s) foreach(i=1:10)%do%sapply(x,FInv,s=s,k=i) and that
works:
FooInv(0.4946433)
F(s = 0.4946433 ,m = 5 ,k = 3 ) equals s; [[1]]
Foreach is a for-loop, and sapply matches FInv for every element of the list
x.
But now another problem occured:
Obviously R gets anywhere a NULL-pointer, because the full output is:
F(s = 0.4946433 ,m = 5 ,k = 3 ) equals s; [[1]]
[[1]][[1]]
NULL
[[1]][[2]]
NULL
[[1]][[3]]
NULL
[[1]][[4]]
NULL
[[1]][[5]]
NULL
...
in every loop.
Do you or someone else know how I can get rid of the NULL-outputs?
if this problem is solved, I have all I need in order to calculate the
distribution and its inverse function :-)
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-tp4707588p4707612.html
Sent from the R help mailing list archive at Nabble.com.