I have data of the following format
Reg_No Subject
AA11 Physics
AA11 Chemistry
AA12 English
AA12 Maths
AA12 Physics
I am trying to transform this data into row wise
Physics Chemistry
English Maths Physics
I know that each student can take maximum of 8 subjects
I am trying to create a matrix that can store the above data as variable rows (each student has different number of subjects)
I have written the following code
# read csv file
Term4 <- read.csv("Term4.csv")
# Find number of Students
Matrix_length <- length(unique(Term4$Reg_No))
# Uniquely store their reg number
Student <- unique(Term4$Reg_No)
# create matrix to be inserted as csv
out <- matrix(NA, nrow=Matrix_length , ncol=8) # max subjects = 8 so ncol =8
# iterate to get each reg number's subjects
for (n in 1:Matrix_length) {
y <- Term4[Term4[,"Reg_No"] == Student[n],]$Subject
# transpose Courses as a single column into row and insert it in the matrix
out[n,] <- t(y)
}
I am getting the following error
Error in out[n, ] <- t(y) :
number of items to replace is not a multiple of replacement length
Could anyone please tell me how to work on this error
Thanks and Regards