Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 12th, 2018 77 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  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]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top