1

Using R, I am working with simulating the outcome from an experiment where participants choose between two options (A or B) defined by their outcomes (x) and probabilities of winning the outcome (p). I have a function "f" that collects its arguments in a matrix with the columns "x" (outcome) and "p" (probability):

f <- function(x, p) {
    t <- matrix(c(x,p), ncol=2)
    colnames(t) <- c("x", "p") 
    t
}

I want to use this function to compile a big list of all the trials in the experiment. One way to do this is:

t1 <- list(1A=f(x=c(10), p=c(0.8)), 
           1B=f(x=c(5), p=c(1)))

t2 <- list(2A=f(x=c(11), p=c(0.8)), 
           2B=f(x=c(7), p=c(1)))
.
.
.

tn <- list(nA=f(x=c(3), p=c(0.8)), 
           nB=f(x=c(2), p=c(1)))

Big_list <- list(t1=t1, t2=t2, ... tn=tn)

rm(t1, t2, ... tn)

However, I have very many trials, which may change in future simulations, why repeating myself in this way is intractable. I have my trials in an excel document with the following structure: | Option | x | p | |---- |------| -----| | A | 10 | 0.8 | | B | 7 | 1 | | A | 9 | 0.8 | | B | 5 | 1 | |... |...| ...|

I am trying to do some kind of loop which takes "x" and "p" from each "A" and "B" and inserts them into the function f, while skipping two rows ahead after each iteration (so that each option is only inserted once). This way, I want to get a set of lists t1 to tn while not having to hardcode everything. This is my best (but still not very good) attempt to explain it in pseudocode:

TRIALS <- read.excel(file_with_trials)

for n=1 to n=(nrows(TRIALS)-1) {
t(*PRINT 'n' HERE*) <- list(
    (*PRINT 'n' HERE*)A=
        f(x=c(*INSERT COLUMN 1, ROW n FROM "TRIALS"*), 
        p=c(*INSERT COLUMN 2, ROW n FROM "TRIALS"*)), 
    (*PRINT 'Z' HERE*)B=
        f(x=c(*INSERT COLUMN 1, ROW n+1 FROM "TRIALS"*), 
        p=c(*INSERT COLUMN 2, ROW n+1 FROM "TRIALS"*)))
}
Big_list <- list(t1=t1, t2=t2, ... tn=tn)

That is, I want the code to create a numbered set of lists by drawing x and p from each pair of rows until my excel file is empty.

Any help (and feedback on how to improve this question) is greatly appreciated!

Mattias
  • 11
  • 1

0 Answers0