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.