[R] BEGINNER: please help me to write my VERY simple function

Peter Wolf s-plus at wiwi.uni-bielefeld.de
Tue Oct 21 12:12:25 CEST 2003


Michele Grassi wrote:

>Hi.
>1)I have two variables: call a<-c(e.g.0,3,6,7...)
>                           b<-c(e.g.6,8,3,4...)
>I want to create a third vector z wich contain the 
>pairs values z<-c(0,6,3,8,6,3,7,4....and so on for each 
>pairs (a,b)).
>There is a specific function?
>How can i write my own function?
>
>2)When i try to write a function and then i save it 
>like "function.R" file, i try to retrieve it with 
>source comand. As result i obtain an error 
>message "error in parse: sintax error on line...". I 
>apply deparse() and i see an incorrect parsing: how 
>avoid unwanted parsing?
>Thanks.
>Michele.
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>  
>
1) a simple solution to the problem:
     define (for example):
     gen.pairs <- function(x,y){
          z<-as.vector(rbind(x,y))
          z
     }
    try:
  >   x<-1:10
  >   y<-11:20
  >  gen.pairs(x,y)
 [1]  1 11  2 12  3 13  4 14  5 15  6 16  7 17  8 18  9 19 10 20

2) a) you can write the definition to a file and then use  source this file.
          If the file name is   myfun.R  
              > source("myfun.R")
           will work. But only if there are no errors in the definition
     b) you can type in the code of the definition after the R prompt ">"
     c) you can type in :
              > gen.pairs <- function(x,y) { z }
          and complete the definition by using edit:
              > edit(gen.pairs)
      However, syntax errors are not allowed!   

Peter




More information about the R-help mailing list