Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare the words from a data frame and calculate a matrix with the length of the biggest word for each pair

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?

like image 844
zero Avatar asked Jul 03 '19 11:07

zero


1 Answers

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)
like image 163
Humpelstielzchen Avatar answered Sep 20 '22 23:09

Humpelstielzchen