Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring the output of a function into a variable in R
    primarykey
    data
    text
    <p>I'm having trouble with storing the output of a function into a variable. I think it's best that I give some context to the problem I'm trying to work out. Suppose that players "a" and "r" play a game of tennis, the runningScoreFn sums the pointHistory vector and puts everything together in a nice data.frame</p> <pre><code>runningScoreFn = function(pointHistory){ playerUni = c("a", "r") cols = sapply(playerUni, function(thisPlayer){ cumsum(pointHistory == thisPlayer) }) names(cols) = playerUni cbind(pointHistory, as.data.frame(cols)) } </code></pre> <p>The oneEpxiermentGameFn that plays out a game of "a" v.s "r".The first player to win 4 points wins the game, but he must be ahead by at least 2 points. "r" has 60% chance of winning a point.</p> <pre><code>pRogerPoint = 0.6 oneExperimentGameFn = function(pRogerPoint){ game = c(rep("r",pRogerPoint * 100), rep("a", 100-pRogerPoint*100)) i = 4 keepGoing = TRUE while(keepGoing){ whosePoint = sample(game, size=i, replace=TRUE) if(sum(whosePoint=="r")-sum(whosePoint=="a")&gt;=2){ success = TRUE print(cbind(runningScoreFn(whosePoint),success=success)) keepGoing = FALSE }else if(sum(whosePoint=="a")-sum(whosePoint=="r")&gt;=2){ success = FALSE print(cbind(runningScoreFn(whosePoint),success=success)) keepGoing = FALSE } i=i+1 } } </code></pre> <p>pRogerGameFn shows the probability that Roger wins the game. </p> <pre><code>pRogerGameFn = function(pRogerPoint, NExperiments){ RogerGameFn = lapply(1:NExperiments,function(dummy){ ok=oneExperimentGameFn(pRogerPoint) })} </code></pre> <p>Here I wish to store the output into the variable ok, but ok returns NULL. I think this has something to do with my oneExperimentGameFn. I also tried ok = RogerGameFn, but ok also returns NULL. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload