Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # read the file with delimiter as `,` and column headers
- data <- read.csv("test.csv", sep = ",", header = TRUE)
- # populating the variables with data
- # only TeacherID and ResourceID are used
- # as.vector is used to vectorize rather than factor
- # mainly to populate the mat matrix
- SysID <- as.vector(data$SystemID)
- TeacherID <- as.vector(data$TeacherKey)
- ResourceID <- as.vector(data$ResourceID)
- # mat matrix keeps track of the TeacherID with ResourceID
- # More efficient way (RFC) : If each row can be called with
- # some index then the creation of mat can be avoided.
- mat = matrix(, nrow = length(TeacherID), ncol = 2)
- mat[,1] <- TeacherID
- mat[,2] <- ResourceID
- # The idea is to construct the binary matrix.
- # We keep a track of unique TeacherID (UTeacherID)
- # and unique Resource ID (UResourceID). Compare these with
- # the mat matrix to fill the BM (Binary Matrix).
- # (RFC) Can binary construction be done more simpler ??
- # The current implementation is too rudimentary as it runs
- # over 3 loops. (WIP : More efficiency)
- # Taking Resource ID as column heads and Teacher ID as row starters.
- UTeacherID <- sort(unique(TeacherID), decreasing = FALSE)
- UResourceID <- sort(unique(ResourceID), decreasing = FALSE)
- BM <- matrix(, nrow = length(UTeacherID), ncol = length(UResourceID))
- for (i in 1:length(UTeacherID)){
- for (j in 1:length(mat[,1])){
- if (mat[j, 1] == UTeacherID[i]){
- for (k in 1:length(UResourceID)){
- if (mat[j, 2] == UResourceID[k]){
- BM[i, k] <- 1
- }
- }
- }
- }
- }
- # NA filled elements have to be made zero
- # WIP : This can be achieved with the above loops
- # RFC : for - else kind in R ??
- for (i in 1:length(UTeacherID)){
- for (j in 1:length(UResourceID)){
- if (is.na(BM[i, j])){
- BM[i, j] <- 0
- }
- }
- }
- # BM matrix construction complete.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement