Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. # read the file with delimiter as `,` and column headers
  2. data <- read.csv("test.csv", sep = ",", header = TRUE)
  3.  
  4. # populating the variables with data
  5. # only TeacherID and ResourceID are used
  6. # as.vector is used to vectorize rather than factor
  7. # mainly to populate the mat matrix
  8.  
  9. SysID <- as.vector(data$SystemID)
  10. TeacherID <- as.vector(data$TeacherKey)
  11. ResourceID <- as.vector(data$ResourceID)
  12.  
  13. # mat matrix keeps track of the TeacherID with ResourceID
  14. # More efficient way (RFC) : If each row can be called with
  15. # some index then the creation of mat can be avoided.
  16.  
  17. mat = matrix(, nrow = length(TeacherID), ncol = 2)
  18. mat[,1] <- TeacherID
  19. mat[,2] <- ResourceID
  20.  
  21. # The idea is to construct the binary matrix.
  22. # We keep a track of unique TeacherID (UTeacherID)
  23. # and unique Resource ID (UResourceID). Compare these with
  24. # the mat matrix to fill the BM (Binary Matrix).
  25. # (RFC) Can binary construction be done more simpler ??
  26. # The current implementation is too rudimentary as it runs
  27. # over 3 loops. (WIP : More efficiency)
  28.  
  29. # Taking Resource ID as column heads and Teacher ID as row starters.
  30.  
  31. UTeacherID <- sort(unique(TeacherID), decreasing = FALSE)
  32. UResourceID <- sort(unique(ResourceID), decreasing = FALSE)
  33. BM <- matrix(, nrow = length(UTeacherID), ncol = length(UResourceID))
  34. for (i in 1:length(UTeacherID)){
  35. for (j in 1:length(mat[,1])){
  36. if (mat[j, 1] == UTeacherID[i]){
  37. for (k in 1:length(UResourceID)){
  38. if (mat[j, 2] == UResourceID[k]){
  39. BM[i, k] <- 1
  40. }
  41. }
  42. }
  43. }
  44. }
  45.  
  46. # NA filled elements have to be made zero
  47. # WIP : This can be achieved with the above loops
  48. # RFC : for - else kind in R ??
  49.  
  50. for (i in 1:length(UTeacherID)){
  51. for (j in 1:length(UResourceID)){
  52. if (is.na(BM[i, j])){
  53. BM[i, j] <- 0
  54. }
  55. }
  56. }
  57. # BM matrix construction complete.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement