Michele Grassi
2003-Oct-21 09:23 UTC
[R] BEGINNER: please help me to write my VERY simple function
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.
Jason Turner
2003-Oct-21 09:52 UTC
[R] BEGINNER: please help me to write my VERY simple function
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? >For that, you don't need to. help(expand.grid)> 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?I'm really not sure what you're trying to do with deparse here, but I don't think it's supposed to do what you meant. The error message is what should be attended to - fix that with your favorite text editor. Cheers Jason -- Indigo Industrial Controls Ltd. http://www.indigoindustrial.co.nz 64-21-343-545 jasont at indigoindustrial.co.nz
Peter Wolf
2003-Oct-21 10:12 UTC
[R] BEGINNER: please help me to write my VERY simple function
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
(Ted Harding)
2003-Oct-21 10:27 UTC
[R] BEGINNER: please help me to write my VERY simple functi
On 21-Oct-03 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?The following will do what you want, though I don't know whether there is a simpler way to do it. z <- rbind(a,b) ; z <- as.vector(z) For example:> a <- c(1,3,5,7) ; b <- c(2,4,6,8) > z <- rbind(a,b) ; z <- as.vector(z) ; z[1] 1 2 3 4 5 6 7 8 Best wishes, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 167 1972 Date: 21-Oct-03 Time: 11:27:02 ------------------------------ XFMail ------------------------------