Guest User

Untitled

a guest
Nov 12th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. library(data.table)
  2. #Create the fictional data up to the current choice.
  3. choices<-c(1:100) #vector of possible choices
  4. people<-data.frame(ID=1:10)
  5. setDT(people,key="ID")
  6. people[,"current_choice":=sample(choices,1),by="ID"] #what the person uses now
  7. people[,"chosen":=sample(choices,1),by="ID"] #what the person actually picked at t_2
  8.  
  9.  
  10.  
  11. #expand the dataset to be 30 rows per person and create a choice ID
  12. people<-people[rep(1:.N,30),]
  13. setDT(people,key="ID")
  14. people[,"choice_id":=seq_len(.N), by="ID"]
  15.  
  16. #The current choice at t_1 needs to be in the choice set
  17. people[1,"choice_set":=current_choice,by="ID"]
  18.  
  19. #The actual choice needs to be in the choice set
  20. people[choice_id==2&current_choice!=chosen,"choice_set":= chosen,by="ID"]
  21.  
  22. #I want the remaining choices to be sampled from the vector of choices, but here is where I'm stuck
  23. people[is.na(choice_set),"choice_set":=sample(choices,1),by="ID]
Add Comment
Please, Sign In to add comment