I have a list of words and their respective lengths. I used adist() to generate a matrix of similarities between the words. Now I wanna divide the similarities by the lengths, so I need to create a matrix of "mean lengths" between each pair of words. How do I do it with apply()? In Excel I would put the list of lengths in the first column and paste it transposed in the first line. Then each cell would be calculated to be the mean between the respective values (first row and first col). But I haven't found how to address the items like that inside apply(). Any thoughts, please? Thanks in advance!
A reproducible example:
lengths <- round(rnorm(10,20,10)) #suppose those are the words' lengths
[1] 30 25 11 5 24 26 10 16 16 9
m <- matrix(ncol=11,nrow=11)
m[1,2:11] <- lengths
m[2:11,1] <- lengths
m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] NA 30 25 11 5 24 26 10 16 16 9
[2,] 30 NA NA NA NA NA NA NA NA NA NA
[3,] 25 NA NA NA NA NA NA NA NA NA NA
[4,] 11 NA NA NA NA NA NA NA NA NA NA
[5,] 5 NA NA NA NA NA NA NA NA NA NA
[6,] 24 NA NA NA NA NA NA NA NA NA NA
[7,] 26 NA NA NA NA NA NA NA NA NA NA
[8,] 10 NA NA NA NA NA NA NA NA NA NA
[9,] 16 NA NA NA NA NA NA NA NA NA NA
[10,] 16 NA NA NA NA NA NA NA NA NA NA
[11,] 9 NA NA NA NA NA NA NA NA NA NA
m[2,2] <- (m[1,2]+m[2,1])/2
m[2,3] <- (m[1,3]+m[2,1])/2
m[2,4] <- (m[1,4]+m[2,1])/2
m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
[1,] NA 30 25.0 11.0 5 24 26 10 16 16 9
[2,] 30 30 27.5 20.5 NA NA NA NA NA NA NA
[3,] 25 NA NA NA NA NA NA NA NA NA NA
[4,] 11 NA NA NA NA NA NA NA NA NA NA
[5,] 5 NA NA NA NA NA NA NA NA NA NA
[6,] 24 NA NA NA NA NA NA NA NA NA NA
[7,] 26 NA NA NA NA NA NA NA NA NA NA
[8,] 10 NA NA NA NA NA NA NA NA NA NA
[9,] 16 NA NA NA NA NA NA NA NA NA NA
[10,] 16 NA NA NA NA NA NA NA NA NA NA
[11,] 9 NA NA NA NA NA NA NA NA NA NA
I need an apply function to to this to the whole matrix. But don't know how to index it.