-1

Hey guys do you know how can I make a list of the pair of elements whose combination is the most frequent? imagine that I have two vectors I perform a cross tabulation and I want to find the pair of those two vectors that is the most frequent. and have it as a list eg (2,3), this means that the element 2 of the first vector and the element 3 of the second vector their combination is the most frequent. for example :

mp<- c(1,1,1,1,2,2,3,4)
mp1<- c("red", "red", "red", "red", "blue", "blue", "green", "pink")

table(mp,mp1)
   mp1
mp  blue green pink red
  1    0     0    0   4
  2    2     0    0   0
  3    0     1    0   0
  4    0     0    1   0

I can see that the most frequent pair is (1, "red") but how can I get that as a result?

nghauran
  • 6,648
  • 2
  • 20
  • 29
cloe
  • 3
  • 4
  • Hello and welcome to StackOverflow. Could you provide a reproducible example? At least an example of vectors. There are some duplicate values in your vectors? – nghauran Oct 21 '17 at 17:06
  • Thanks. I want to put that on a function. I will provide the function with two equal length vectors and it will compute the cross tabulation and I want as a return the two element that their combination is the most frequent. eg – cloe Oct 21 '17 at 17:11
  • v1<- c(2,3,2,3,5,6,7,8) , v2<- c(2,2,2,2,3,4,5,6) – cloe Oct 21 '17 at 17:12
  • but they re not gonna be only numeric it could be for example v2<- c("blue", "red", "blue", "green") etc – cloe Oct 21 '17 at 17:13
  • Ok I got you right but you want the position in the vector as a result so how do you want to deal with "blue" for example which is the first and second element of v2? – nghauran Oct 21 '17 at 17:17
  • no I don't want the position I want the exact element. so I need to keep the type of each vector as a result. so eg (2, "blue") – cloe Oct 21 '17 at 17:19

2 Answers2

0

You can design your function in this way

v1 <- c(2,3,2,3)
v2 <- c("blue", "red", "blue", "green") 
tbl <- table(v1, v2)
tbl # most frequent combination: 2 & blue
max(tbl) # most frequent combination = 2
indices = which(tbl == max(tbl), arr.ind = TRUE) # indices for row and column of
indices                                          # the max value
v1.val = rownames(tbl)[indices[, 1]]
v2.val = colnames(tbl)[indices[, 2]]
res = c(v1.val, v2.val)
res # result

It should also work with

v1 <- c(1,1,1,1,2,2,3,4)
v2 <- c("red", "red", "red", "red", "blue", "blue", "green", "pink")

Note that adding a character string to a numeric vector converts all the elements in the vector to character. Here the result will be a character vector.

nghauran
  • 6,648
  • 2
  • 20
  • 29
  • can't I keep for each element its original form by adding typeof?? – cloe Oct 21 '17 at 18:12
  • It will not be possible if the type of the vectors are different. Try this `a <- c("blue", 1)`; if you print `a`, you can see that R returns `"blue" "1" ` because you cannot combine numeric and character in the same vector. `typeof(a)` will return `character` since all the elements of the vector have been converted in `character` – nghauran Oct 21 '17 at 18:17
0

Firstly, make a dataframe from your vectors:

mp<- c(1,1,1,1,2,2,3,4)
mp1<- c("red", "red", "red", "red", "blue", "blue", "green", "pink")
df <- data.frame(mp, mp1)

Then you have many options to count combinations. I personally prefer dplyr, so:

count_table <- dplyr::count_(df, vars = c('mp','mp1'))
count_table
# A tibble: 4 x 3
     mp    mp1     n
  <dbl> <fctr> <int>
1     1    red     4
2     2   blue     2
3     3  green     1
4     4   pink     1

You can output only the first row:

count_table[1,]
# A tibble: 1 x 3
     mp    mp1     n
  <dbl> <fctr> <int>
1     1    red     4
Alex Knorre
  • 620
  • 4
  • 15