I have the following example of something that i've been doing, which is formally simple, but I wanted to check what are the potential alternatives to my code -- in order to get faster, if possible. Here it is the example:
Time1=Sys.time()
v=rep(c("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"),
each=1000)
m=matrix(0,ncol=length(v),nrow=length(v))
for (j in 1:length(v)) {
for(i in 1:length(v)) {
if (v[j]==v[i]) {
m[j,i]=1
} else {
next
}
}
}
Time2=Sys.time()
Time2-Time1
# Time difference of 1.405404 mins
I am creating a simple relational matrix -- where the vector v1 could be interpreted as being placed as lines and columns and the matrix maps where the results are equal. If they're equal, we get m[j,i]=1; if not equal, m[j,i]=0. As I stated, I would like to make this code go faster. I was trying think of a way to code it as an apply function, but I haven't figured that out for now. Still, I would like to know if there are other options besides what I've said.
EDIT: I made some corrections on the text and I tried to clarify the question.