1
I’m doing a class exercise, but I believe you’re doing it the hard way and I’d like to know if there’s an easier way. This is the matrix I need to work on. The final matrix needs to have the matrix values be a column, if in any of the rows the observation that is the column is present, the value in the row gets 1, otherwise 0.
my.matrix <- matrix(c("A","A","G","G","I","B","B","A","A","J","C","C","D","D","A","E","E","H","H","D","F","F","B","B","F"), ncol = 5, nrow = 5)
To visualize the problem, this is the data.frame that I need to create.
my.new.matrix <- matrix(c(1,1,1,1,1,1,1,1,1,0, 1,1,0,0,0, 0,0,1,1,1, 1,1,0,0,0,1,1,0,0,1, 0,0,1,1,0, 0,0,1,1,0,0,0,0,0,1, 0,0,0,0,1), ncol = 10, nrow = 5)
my.new.matrix <- data.frame(my.new.matrix)
names(my.new.matrix) <- c("A","B","C","D","E","F","G","H","I","J")
my.new.matrix
I didn’t like the ways I created them, as they are repetitive and almost at hand and would be unusual for a longer data.frame. I wonder if you have any easy way to do the task.