I have a data frame with a number of unique words. I want to create code in R, where each word will be compared with all the words and creates a matrix with the length of the biggest word from each pair.
To be more comprehensive lets consider the follow example.
test <- c("hello", "hi", "play", "kid")
I want to create a matrix that compares each word in the test and gives me the length of the biggest word.
For the previous example I want to take the below matrix:
hello hi play kid
hello 5 5 5 5
hi 5 2 4 3
play 5 4 4 4
kid 5 3 4 3
How Can I do it in R?
You can do this:
outer(test, test, function(x,y) pmax(nchar(x), nchar(y)))
[,1] [,2] [,3] [,4]
[1,] 5 5 5 5
[2,] 5 2 4 3
[3,] 5 4 4 4
[4,] 5 3 4 3
Or even shorter, as suggested by @Ronak Shah
outer(nchar(test), nchar(test), pmax)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With