I have a dataset which looks like:
title | text | type | id |
---|---|---|---|
A | I like... | NA | 67340 |
C | I wish... | Question | c912 |
C | review1 | Replies | a7869 |
C | review2 | Replies | 5769 |
C | review3 | Answer | 67340 |
C | Detecting... | Answer | 28524 |
C | review1 | Answer | 80445 |
B | Let's.. | NA | 4fcd |
C | New... | Replies | 5769 |
I want to create a data frame for a graph something like:
from | to | relation | description |
---|---|---|---|
67340 | A | Asked | I like... |
c912 | 67340 | Commented | I wish... |
a7869 | 67340 | Replied | review1 |
5769 | 67340 | Replied | review2 |
67340 | 5769 | Answered | review3 |
28524 | 5769 | Answered | Detecting... |
80445 | 5769 | Answered | review1 |
4fcd | B | Asked | Let's.. |
5769 | New... | Replied | New... |
The B
in the title is a different network from the above.
I tried to add columns but don't know what conditions to put to create such columns with values.
df %>%
mutate(from = ifelse(type == ____, ___, ____))
I want to create a network from the above something like
CodePudding user response:
library(tidyverse)
df <- tribble(~title, ~text, ~type, ~id,
"A", "I like...", NA, "67340",
"C", "I wish...", "Question", "c912",
"C", "review1", "Replies", "a7869",
"C", "review2", "Replies", "5769",
"C", "review3", "Answer", "67340",
"C", "Detecting...", "Answer", "28524",
"C", "review1", "Answer", "80445",
"B", "Let's..", NA, "4fcd",
"C", "New...", "Replies", "5769")
df %>% mutate(conversation_group = na_if(title, "C")) %>%
fill(conversation_group) %>%
group_by(conversation_group) %>%
transmute(from = id,
to = replace_na(lag(id), first(conversation_group)),
relation = case_when(is.na(type) ~ "Asked",
type == "Question" ~ "Commented",
type == "Replies" ~ "Replied",
type == "Answer" ~ "Answered"),
description = text) %>%
ungroup() %>%
select(-conversation_group)
#> # A tibble: 9 × 4
#> from to relation description
#> <chr> <chr> <chr> <chr>
#> 1 67340 A Asked I like...
#> 2 c912 67340 Commented I wish...
#> 3 a7869 c912 Replied review1
#> 4 5769 a7869 Replied review2
#> 5 67340 5769 Answered review3
#> 6 28524 67340 Answered Detecting...
#> 7 80445 28524 Answered review1
#> 8 4fcd B Asked Let's..
#> 9 5769 4fcd Replied New...
Created on 2022-06-18 by the reprex package (v2.0.1)