Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(data.table)
- #Create the fictional data up to the current choice.
- choices<-c(1:100) #vector of possible choices
- people<-data.frame(ID=1:10)
- setDT(people,key="ID")
- people[,"current_choice":=sample(choices,1),by="ID"] #what the person uses now
- people[,"chosen":=sample(choices,1),by="ID"] #what the person actually picked at t_2
- #expand the dataset to be 30 rows per person and create a choice ID
- people<-people[rep(1:.N,30),]
- setDT(people,key="ID")
- people[,"choice_id":=seq_len(.N), by="ID"]
- #The current choice at t_1 needs to be in the choice set
- people[1,"choice_set":=current_choice,by="ID"]
- #The actual choice needs to be in the choice set
- people[choice_id==2¤t_choice!=chosen,"choice_set":= chosen,by="ID"]
- #I want the remaining choices to be sampled from the vector of choices, but here is where I'm stuck
- people[is.na(choice_set),"choice_set":=sample(choices,1),by="ID]
Add Comment
Please, Sign In to add comment