R: Having trouble getting frequency of matching emoji pairs within data frame

I am having a problem getting the frequency of similar pair of emojis in R. I don't know if there is a logic error. My code segment follows like this:

emoji_pouch <- c("๐Ÿ˜ƒ", "๐Ÿ™„", "๐Ÿง•", "๐Ÿค”", "๐Ÿ‘", "๐Ÿ‘", "๐Ÿ‘", "๐Ÿ‘", "๐Ÿ‘", "๐Ÿ˜ข", "๐Ÿ‡จ๐Ÿ‡ฆ", "๐Ÿคฃ", "๐Ÿ˜", "๐Ÿ™", "๐Ÿ™", "๐Ÿ™", "๐Ÿ™Œ", "๐Ÿ™Œ", "๐Ÿ™Œ", "๐Ÿ‘", "๐Ÿ‘", "๐Ÿ‘")
emo_mat <- matrix(emoji_pouch, ncol = 2, byrow = T)
emo_mat
#     [,1] [,2]
#[1,] "๐Ÿ˜ƒ"  "๐Ÿ™„" 
#[2,] "๐Ÿง•"  "๐Ÿค”" 
#[3,] "๐Ÿ‘"  "๐Ÿ‘" 
#[4,] "๐Ÿ‘"  "๐Ÿ‘" 
#[5,] "๐Ÿ‘"  "๐Ÿ˜ข" 
#[6,] "๐Ÿ‡จ๐Ÿ‡ฆ" "๐Ÿคฃ" 
#[7,] "๐Ÿ˜"  "๐Ÿ™" 
#[8,] "๐Ÿ™"  "๐Ÿ™" 
#[9,] "๐Ÿ™Œ"  "๐Ÿ™Œ" 
#[10,] "๐Ÿ™Œ"  "๐Ÿ‘" 
#[11,] "๐Ÿ‘"  "๐Ÿ‘" 

links <- data.frame(source= emo_mat[,1], target= emo_mat[,2])

weight_col_count <- c()

#Possible logic error which I can't find
for(i in 1:nrow(links))
{
  weight_counter <- 1
  
  for(j in 1: nrow(links))
  {
    if(links[i, ] == links[j, ]) #I am not sure if I doing this line correctly??
    {
      weight_counter <- weight_counter + 1
    }
  }

  weight_col_count[i] <- weight_counter
}

links$weight <- weight_col_count
links 

# gives me output like this:
#   source target weight
#1       ๐Ÿ˜ƒ      ๐Ÿ™„      2
#2       ๐Ÿง•      ๐Ÿค”      2
#3       ๐Ÿ‘      ๐Ÿ‘      5
#4       ๐Ÿ‘      ๐Ÿ‘      5
#5       ๐Ÿ‘      ๐Ÿ˜ข      5
#6      ๐Ÿ‡จ๐Ÿ‡ฆ      ๐Ÿคฃ      2
#7       ๐Ÿ˜      ๐Ÿ™      2
#8       ๐Ÿ™      ๐Ÿ™      2
#9       ๐Ÿ™Œ      ๐Ÿ™Œ      3
#10      ๐Ÿ™Œ      ๐Ÿ‘      3
#11      ๐Ÿ‘      ๐Ÿ‘      5

#Desired output would be:
#   source target weight
#1       ๐Ÿ˜ƒ      ๐Ÿ™„      1
#2       ๐Ÿง•      ๐Ÿค”      1
#3       ๐Ÿ‘      ๐Ÿ‘      3
#4       ๐Ÿ‘      ๐Ÿ‘      3
#5       ๐Ÿ‘      ๐Ÿ˜ข      1
#6      ๐Ÿ‡จ๐Ÿ‡ฆ      ๐Ÿคฃ      1
#7       ๐Ÿ˜      ๐Ÿ™      1
#8       ๐Ÿ™      ๐Ÿ™      1
#9       ๐Ÿ™Œ      ๐Ÿ™Œ      1
#10      ๐Ÿ™Œ      ๐Ÿ‘      1
#11      ๐Ÿ‘      ๐Ÿ‘      3

I have looked Stack Overflow community thoroughly, but can't seem to find anything helpful. To be honest, it could be due to my limited knowledge on the subject. This is my side project, and I am using emo, and remotes packages.

You can get the frequency for every pair of emojis using the dplyr package. I think it is an easier solution than using loops and counters. The only difference with your approach is that every combination of emojis appears only once. Hope this is helpful! Here is my solution:

library(dplyr)

links %>%
  group_by(source,target) %>% #group by both variables
  count()#Get frequencies for every combination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
็ฒคICPๅค‡18138465ๅท  © 2020-2024 STACKOOM.COM