This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(readr) | |
library(igraph) | |
friends_network <- function(season = NULL, edgelist = NULL, type = c("mds", "sphere")) { | |
friends <- c("Phoebe", "Monica", "Rachel", "Joey", "Ross", "Chandler") | |
edgelist_matrix <- as.matrix(edgelist[ ,c("from", "to")]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(jpeg) | |
source("network_analysis/friends_network.R") | |
# get friends full series edgelist | |
edgefile_url <- "https://github.com/keithmcnulty/friends_analysis/blob/master/data/friends_edgelist_by_season.RDS?raw=true" | |
download.file(edgefile_url, "edgelist.RDS") | |
edgelist <- readRDS("edgelist.RDS") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(igraph) | |
library(tidyverse) | |
library(networkD3) | |
library(jsonlite) | |
graph_json <- function(season = NULL, edgelist = NULL) { | |
edgelist_matrix <- as.matrix(edgelist[ ,c("from", "to")]) | |
# create igraph network |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
storms_sum <- storms %>% | |
dplyr::filter(year %in% 1975:1977) %>% | |
dplyr::group_by(year, status) %>% | |
dplyr::summarise(mean = mean(pressure, na.rm = TRUE), | |
median = median(pressure, na.rm = TRUE)) | |
storms_sum | |
#> # A tibble: 9 x 4 | |
#> # Groups: year [3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
storms_sum %>% | |
tidyr::pivot_wider(names_from = "year", values_from = c("mean", "median")) | |
#> # A tibble: 3 x 7 | |
#> status mean_1975 mean_1976 mean_1977 median_1975 median_1976 median_1977 | |
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> | |
#> 1 hurricane 977. 975. 978. 984 975 987 | |
#> 2 tropical de… 1011. 1007. 1011. 1012. 1006. 1010 | |
#> 3 tropical st… 992. 995. 1002. 993 994. 1001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wide_storms <- storms_sum %>% | |
tidyr::pivot_wider(names_from = "year", values_from = c("mean", "median"), | |
names_glue = "{.value}_of_{year}") | |
wide_storms | |
#> # A tibble: 3 x 7 | |
#> status mean_of_1975 mean_of_1976 mean_of_1977 median_of_1975 median_of_1976 | |
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> | |
#> 1 hurri… 977. 975. 978. 984 975 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
wide_storms %>% | |
tidyr::pivot_longer(-status, names_to = c("stat", "year"), | |
names_pattern = "(.*)_of_(.*)", | |
values_to = "value") | |
#> # A tibble: 18 x 4 | |
#> status stat year value | |
#> <chr> <chr> <chr> <dbl> | |
#> 1 hurricane mean 1975 977. | |
#> 2 hurricane mean 1976 975. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mtcars %>% | |
dplyr::group_by(cyl) %>% | |
dplyr::summarise_at(vars(starts_with("d")), | |
list(mean = mean, median = median)) | |
#> # A tibble: 3 x 5 | |
#> cyl disp_mean drat_mean disp_median drat_median | |
#> <dbl> <dbl> <dbl> <dbl> <dbl> | |
#> 1 4 105. 4.07 108 4.08 | |
#> 2 6 183. 3.59 168. 3.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mtcars %>% | |
dplyr::group_by(cyl) %>% | |
dplyr::summarise(across(starts_with("d"), | |
list(mean = mean, median = median), | |
.names = "{fn}_of_{col}")) | |
#> # A tibble: 3 x 5 | |
#> cyl mean_of_disp median_of_disp mean_of_drat median_of_drat | |
#> <dbl> <dbl> <dbl> <dbl> <dbl> | |
#> 1 4 105. 108 4.07 4.08 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mtcars %>% | |
nest_by(cyl) | |
#> # A tibble: 3 x 2 | |
#> # Rowwise: cyl | |
#> cyl data | |
#> <dbl> <list<tbl_df[,10]>> | |
#> 1 4 [11 × 10] | |
#> 2 6 [7 × 10] | |
#> 3 8 [14 × 10] |
OlderNewer