Reputation: 351
I am trying to create a conditional sum for each row in my data frame according two different lists objects. The idea is that check the row value/values (some of them are list object) and assign 1 or -1 for each value/values and find the total for it/them.
For example; df row 7 has c("cuneytozdemir", "ahmethc", "fatihportakal")
like this and side value of this row should be 1 because of that cuneytozdemir: 1, ahmethc: -1, fatihportakal:1
I have tried this code and I only got results for single value and I haven't gotten any value for list object.
df_side <- df %>%
mutate(side= case_when(
mentions %in% unlist(J_Con) ~ 1,
mentions %in% unlist(J_Pro) ~ -1))
Sample of data frame in here:
df <- structure(list(screen_name = c("N_bingol", "SAliakman", "TC_SelimCandas",
"deniyeciz", "batuozcn", "dst_cemal", "kentineiyibak", "GokhanMHP",
"GokhanMHP", "ramadanmeletli"),
mentions = list(c("turgayguler","mecertas", "yasingorgulu28", "Atasehirakgenc", "SAliakman"),
c("N_bingol", "turgayguler", "mecertas", "yasingorgulu28","Atasehirakgenc", "SAliakman"),
c("MesutKokten", "ahmethc","MHP_Bilgi"), "ahmethc",
c("tevfik_uygur", "turgayguler"),"hikmetgenc",
c("cuneytozdemir", "ahmethc", "fatihportakal"), "ahmethc", "ahmethc",
c("GokhanMHP", "ahmethc"))),
row.names = c("11355","11383", "68592", "228341", "354369", "358484", "359043", "371744","371753", "372216"),
class = "data.frame")
J_Con and J_Pro are in here:
J_Con <- structure(c(17L, 26L, 31L, 38L, 61L, 10L, 68L, 13L, 24L, 41L,
48L, 56L, 49L, 22L, 25L, 52L, 39L, 5L, 9L, 33L, 69L, 73L, 66L,
28L, 55L, 8L, 1L, 74L, 47L, 70L, 20L, 4L, 32L, 16L, 65L, 6L,
14L, 21L, 63L, 43L, 53L, 57L, 15L, 36L, 7L, 37L, 50L, 54L, 12L),
.Label = c("acikcenk", "ahmetay_", "ahmethc", "akdemircigdem",
"amberinzaman", "arzuyldzz", "asliaydintasbas", "AtillaSertell",
"banuguven", "barisyarkadas", "bekiservet", "cakir_rusen", "candundaradasi",
"cengizalgan", "ceydak", "cuneytozdemir", "degirmencirfan", "denizyildirim79",
"doganburak29", "dunya20101", "efekerem", "emrkongar", "EremSenturk",
"erenerdemnet", "ETemelkuran", "fatihportakal", "fatihtezcan",
"gokhanozbek", "haciykk", "hakanchelik", "Halitisci", "HasanCucuk",
"haykobagdat", "hikmetgenc", "hilal_kaplan", "imambakirukus",
"iremafsin", "ismaildukel", "ismailsaymaz", "kenan_kiran", "KucukkayaIsmail",
"mahmutovur", "MaliGuller", "Malikejder47", "MehToprak", "melihaltinok",
"merdanyanardag", "mkirikkanat", "mustafabalbay", "mustafahos",
"nevzatcicek", "nihatsirdar", "NuranWolf", "ozge_mumcu", "ozgurmumcu",
"RuhatMengi34", "s_hablemitoglu", "samiltayyar27", "Sarikli_Voyvoda",
"sarseven", "SedefKabas", "sevilayyaziyor", "siring", "slymnoz",
"TaylanKulacogIu", "Tuluhantekeli", "turgayguler", "ugurdundarsozcu",
"unsalunlu", "utkucakirozer", "veyisates", "yenisafakwriter",
"yvzah", "YZGLLDGN", "zihnicakir"), class = "factor")
J_Pro <- structure(c(67L, 27L, 11L, 40L, 75L, 34L, 3L, 44L, 64L, 72L,
35L, 58L, 60L, 71L, 18L, 62L, 19L, 46L, 51L, 2L, 23L, 29L, 30L,
42L, 59L, 45L),
.Label = c("acikcenk", "ahmetay_", "ahmethc","akdemircigdem", "amberinzaman",
"arzuyldzz", "asliaydintasbas","AtillaSertell", "banuguven", "barisyarkadas",
"bekiservet","cakir_rusen", "candundaradasi", "cengizalgan", "ceydak", "cuneytozdemir",
"degirmencirfan", "denizyildirim79", "doganburak29", "dunya20101",
"efekerem", "emrkongar", "EremSenturk", "erenerdemnet", "ETemelkuran",
"fatihportakal", "fatihtezcan", "gokhanozbek", "haciykk", "hakanchelik",
"Halitisci", "HasanCucuk", "haykobagdat", "hikmetgenc", "hilal_kaplan",
"imambakirukus", "iremafsin", "ismaildukel", "ismailsaymaz",
"kenan_kiran", "KucukkayaIsmail", "mahmutovur", "MaliGuller",
"Malikejder47", "MehToprak", "melihaltinok", "merdanyanardag",
"mkirikkanat", "mustafabalbay", "mustafahos", "nevzatcicek",
"nihatsirdar", "NuranWolf", "ozge_mumcu", "ozgurmumcu", "RuhatMengi34",
"s_hablemitoglu", "samiltayyar27", "Sarikli_Voyvoda", "sarseven",
"SedefKabas", "sevilayyaziyor", "siring", "slymnoz", "TaylanKulacogIu",
"Tuluhantekeli", "turgayguler", "ugurdundarsozcu", "unsalunlu",
"utkucakirozer", "veyisates", "yenisafakwriter", "yvzah", "YZGLLDGN",
"zihnicakir"), class = "factor")
Upvotes: 1
Views: 139
Reputation: 23574
Reading your question and code, it seems to me that you want to do something like the following. You took row 7 as an example. You tried to check whether each name in that row appears in J_pro
or J_Con
. If the name appears in J_Con
assign 1 to the name. If the name appears in J_Pro
assign -1 to the name. Otherwise assign 0 to the name. I think you were trying to do that in your code. Then, for each row you wanted to sum up values from both logical checks.
Given that, first, I created a group variable called id
then converted the data to a long-format data. I created two columns to do the logical check. Then, I summed up the two columns. Finally, I removed the columns. If you need to further aggregate the data by screen_name, you need one more group_by + summarize
library(dplyr)
library(tidyr)
library(purrr)
# Check if there is any overlapping between the two.
intersect(J_Pro, J_Con)
group_by(df, id = 1:n()) %>%
unnest_longer(mentions) %>%
mutate(check_con = if_else(mentions %in% J_Con, 1, 0),
check_pro = if_else(mentions %in% J_Pro, -1, 0)) %>%
summarize(screen_name = first(screen_name),
res = sum(check_con) + sum(check_pro)) %>%
select(-contains("check"))
# id screen_name res
# <int> <chr> <dbl>
# 1 1 N_bingol -1
# 2 2 SAliakman -1
# 3 3 TC_SelimCandas -1
# 4 4 deniyeciz -1
# 5 5 batuozcn -1
# 6 6 dst_cemal -1
# 7 7 kentineiyibak 1
# 8 8 GokhanMHP -1
# 9 9 GokhanMHP -1
#10 10 ramadanmeletli -1
Another idea is the following. For each list in mentions
, create a character vector using unlist()
, run a logical check, and sum up values. This process is done twice (one for J_con and the other for J_Pro). The returned value is going to each row of res
.
mutate(df,
res = map(.x = mentions,
.f = function(x) {sum(if_else(unlist(x) %in% J_Con, 1, 0)) +
sum(if_else(unlist(x) %in% J_Pro, -1, 0))}))
# screen_name mentions res
#1 N_bingol turgayguler, mecertas, yasingorgulu28, Atasehirakgenc, SAliakman -1
#2 SAliakman N_bingol, turgayguler, mecertas, yasingorgulu28, Atasehirakgenc, SAliakman -1
#3 TC_SelimCandas MesutKokten, ahmethc, MHP_Bilgi -1
#4 deniyeciz ahmethc -1
#5 batuozcn tevfik_uygur, turgayguler -1
#6 dst_cemal hikmetgenc -1
#7 kentineiyibak cuneytozdemir, ahmethc, fatihportakal 1
#8 GokhanMHP ahmethc -1
#9 GokhanMHP ahmethc -1
#10 ramadanmeletli GokhanMHP, ahmethc -1
Upvotes: 2