简体   繁体   中英

Assigning a value to the global variable (and a argument of a function aswell) R

I am currently working on my master thesis (model of a evolutionary trust game). And I have a following problem. I have several types of investors and trustees that are paired to play the trust game. Investors and trustees are defined as vectors. What I would like my code(trust game) to do is that it takes the (globally defined) investor and trustee. They play together the iteration of the trust game and then the globally defined variables are updated inside the function trust game. I would like that it would work for any investor\\trustee that is used as the argument of the function trustgame. Do you know how could I code this? I am not sure aswell if it will help you but I post my code aswell.

    #### defining trustees ####
# [1] honor\abuse
# [2] information about previous interaction - relevant to investors who buy the information
# [3] payoff
# [4] number of interactions
trustee1 <- c(1,0,0,0)
#### defining investors ####
# [1] buy\not buy
# [2-4] investment decision if buying information in case 1(TH), case 2(TA), case 3(NT)
# [5] aggregated payoff
# [6] number of interactions in one generation
investor1 <- c(1,1,1,1,0,0)


here is the code for the trust game

trustgame <- function(investor,trustee)
{ investordecision <- NULL
trusteedecision <- trustee[1]
investor[6] <- investor[6]+1
trustee[4] <- trustee[4]+1
if (investor[1]==0) investordecision <- investor[2]
if (investor[1]==1)
{ if (trustee[2]==1) investordecision <- investor[2]
if (trustee[2]==2) investordecision <- investor[3]
if (trustee[2]==3) investordecision <- investor[4]
if (trustee[2]==0) investordecision <- rbinom(1,1,0.5)
}
if (investordecision==1 && trustee[2]==1) trustee[2] <- 1
if (investordecision==1 && trustee[2]==0) trustee[2] <- 2  
if (investordecision==0) trustee[2] <- 3

if (investordecision==1 && trusteedecision==1)
{trustee[3] <- trustee[3] +3
investor[5] <- investor[5] + 3 }
if (investordecision==1 && trusteedecision==0)
{trustee[3] <- trustee[3] +5
investor[5] <- investor[5] + 0 }
if (investordecision==0 && trusteedecision==0)
{trustee[3] <- trustee[3] +1
investor[5] <- investor[5] + 1 }
if (investordecision==0 && trusteedecision==1)
{trustee[3] <- trustee[3] +1
investor[5] <- investor[5] + 1 }

}

If your inputs are strings, you can use get and assign to change the values in the global environment like this:

trustee1 <- c(1,0,0,0)
investor1 <- c(1,1,1,1,0,0)

trustgame <- function(investor_string,trustee_string){ 
  investor <- get(investor_string, envir = globalenv())
  trustee <- get(trustee_string, envir = globalenv())
  investordecision <- NULL
  trusteedecision <- trustee[1]
  investor[6] <- investor[6]+1
  trustee[4] <- trustee[4]+1
  if (investor[1]==0) investordecision <- investor[2]
  if (investor[1]==1){ 
    if (trustee[2]==1) investordecision <- investor[2]
    if (trustee[2]==2) investordecision <- investor[3]
    if (trustee[2]==3) investordecision <- investor[4]
    if (trustee[2]==0) investordecision <- rbinom(1,1,0.5)
  }
  if (investordecision==1 && trustee[2]==1) trustee[2] <- 1
  if (investordecision==1 && trustee[2]==0) trustee[2] <- 2  
  if (investordecision==0) trustee[2] <- 3

  if (investordecision==1 && trusteedecision==1){
    trustee[3] <- trustee[3] +3
    investor[5] <- investor[5] + 3 
  }
  if (investordecision==1 && trusteedecision==0){
    trustee[3] <- trustee[3] +5
    investor[5] <- investor[5] + 0 
  }
  if (investordecision==0 && trusteedecision==0){
    trustee[3] <- trustee[3] +1
    investor[5] <- investor[5] + 1 
  }
  if (investordecision==0 && trusteedecision==1){
    trustee[3] <- trustee[3] +1
    investor[5] <- investor[5] + 1 
  }
  assign(trustee_string, trustee, envir = globalenv())
  assign(investor_string, investor, envir = globalenv())
}

trustgame("investor1", "trustee1")
> investor1
[1] 1 1 1 1 1 1
> trustee1
[1] 1 3 1 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM