[R] functions without arguments
Petr Klasterecky
klaster at karlin.mff.cuni.cz
Wed May 30 11:27:31 CEST 2007
Sure it is possible, but it is a very bad and possibly dangerous idea,
since you may not control what's passed to your function. Consider this
example:
> x <- 1:5
> x
[1] 1 2 3 4 5
> foo <- function(){mean(x)}
> foo()
[1] 3
> rm('x')
> foo()
Error in mean(x) : object "x" not found
It is much much beter to include argument(s) and to set them to some
default value(s):
> foo <- function(x=c(2,3,1,1,4,5,1)){mean(x)}
> foo()
[1] 2.428571
> foo(1:10)
[1] 5.5
>
Petr
elyakhlifi mustapha napsal(a):
> hello,
> I wanna know if it's possible to write functions without argument
> in order to understand that I write down an example
>
> f <- function(){
> for (i in 1:length(C[[1]]) {
> print(10*C[[1]][i])
> }
> }
>
> I know that there are errors in this syntax
>
>
> _____________________________________________________________________________
>
> [[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
> and provide commented, minimal, self-contained, reproducible code.
>
--
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic
More information about the R-help
mailing list