[R] R-help
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Mon May 18 17:51:33 CEST 2015
It is quite clear that you are confused, because your function is full of variables that are not defined in your email. Unfortunately, that also makes helping you quite difficult. Also, your (accidental?) use of HTML format means that your code is garbled on our end (see below). You have to learn how to make your email program send plain text to avoid this problem. (This point and others are in the Posting Guide mentioned at the bottom if this and every email on the list.
Usually your functions will be easier to understand and run faster if they return a vector, and at the point where you call the function you assign that return value to a new column in your data table. Normally we just put an expression (often as simple as a variable name) alone on the last line of the function in order to return it.
plusXY <- function( x, y ) {
result <- x + y
result
}
DF <- data.frame( a=1:5, b=5:9 )
str( DF )
DF$c <- plusXY( DF$a, DF$b )
str( DF )
The above steps are a reproducible example. Without you giving us a reproducible example of the kind of code AND data you have it is very difficult to understand your problem at the receiving end of an email. The link [1] below gives advice on making such examples.
I also recommend that you (more carefully) read the Introduction to R document that came with the software. In particular, study indexing or the various members of the lapply family of functions, since rbind is quite an inefficient way to add data to a data frame. Patrick Burns' "The R Inferno" [2] may provide some useful guidance as well.
[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
[2] http://www.burns-stat.com/
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On May 18, 2015 4:32:20 AM PDT, "Ariadna García Sáenz" <quiro3 at msn.com> wrote:
>Good afternoon,
>I'm working in a disease spread model, and I have to use a function to
>put information on a table.
>My question is that I don't know how to get the output of this function
>in a table. Thanks in advance!
>Here I paste my script:
>tbmodel <- function(time,S,E,I,De,Di,beta,alpha,phi,rho){ ts <-
>data.frame(time=sim.time,S=susceptibles,E=exposed,I=infectious,De=De,Di=Di)
>next.time <- ts
>while(next.time$time<limit.time&next.time$S>0&next.time$E>=0&next.time$I>=0)
># Conditions under which within-herd spread continues { next.time
><-gillespie(next.time$time,next.time$S,next.time$E,
>next.time$I,next.time$De,next.time$Di,beta,alpha,phi,rho) ts <-
>rbind(ts,next.time)
>if(ts$time[nrow(ts)]>limit.time|ts$E[nrow(ts)]<0)ts <- ts[-nrow(ts),]
># Elimination of last column (time> limit.time) }
>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>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