-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#' @title Retrieve Timeline | ||
#' @description Timeline data for selected variable (possibly across various groups). | ||
#' @param x data frame | ||
#' @param field Numeric field to summarize in the timeline. The number of entries (title count) per decade is used by default. If this argument is used, the sum of entries per decade for this field is given. | ||
#' @param group Optional. Name for a data field that indicates groups to compare. | ||
#' @param nmin Include only entries with at least nmin absolute frequency | ||
#' @param mode "absolute" or "relative" | ||
#' @param time.window Time window for the timeline in years. Default: 10 (publication decade). | ||
#' @param time.field Specify the field to be used for time. By default: "Year", or if time.window is 10, then "publication_decade" | ||
#' @return data.frame | ||
#' @importFrom tidyr pivot_wider | ||
#' @importFrom reshape2 melt | ||
#' @export | ||
#' @author Leo Lahti \email{leo.lahti@@iki.fi} | ||
#' @references See citation("bibliographica") | ||
#' @examples \dontrun{timeline(df, "gatherings")} | ||
#' @keywords utilities | ||
timeline <- function (x, field = "titlecount", group = NULL, nmin = 0, mode = "absolute", time.window = 10, time.field = "Year") { | ||
|
||
publication_decade <- publication_time <- NULL | ||
|
||
x$publication_time <- x[[time.field]] | ||
|
||
# Set the desired time window (default one decade) | ||
x$publication_time <- time.window * floor(x$publication_time / time.window) | ||
|
||
|
||
if (time.field == "publication_decade" || (time.field == "Year" & time.window == 10 & "publication_decade" %in% names(x))) { | ||
x$publication_time <- x$publication_decade | ||
} | ||
|
||
if (!is.null(group)) { | ||
x <- x[, c("publication_time", group)] | ||
x$group <- x[[group]] | ||
} else { | ||
x$group <- rep(1, nrow(x)) | ||
} | ||
|
||
if (is.null(field)) { | ||
field <- "titlecount" | ||
} | ||
|
||
if (field == "titlecount" && !field %in% names(x)) { | ||
x[[field]] <- rep(1, nrow(x)) | ||
} | ||
|
||
x$field <- x[[field]] | ||
#print(x) | ||
|
||
df2 <- x %>% filter(!is.na(group)) %>% | ||
group_by(publication_time, group) %>% | ||
summarise(absolute = sum(field, na.rm = TRUE)) | ||
|
||
# Remove entries with too few occurrences | ||
df2 <- df2 %>% filter(!is.na(publication_time) & | ||
group %in% setdiff(unique(as.character(unname(unlist(df2[which(df2$absolute >= nmin), "group"])))), "NA")) | ||
df2$group <- factor(df2$group) | ||
df2$group <- droplevels(df2$group) | ||
|
||
# Add relatives | ||
df3 <- pivot_wider(df2, names_from = "publication_time", values_from = "absolute", values_fill = 0) | ||
df3 <- as.data.frame(df3) | ||
df3[, -1] = 100 * apply(df3[, -1], 2, function (x) {x/sum(x, na.rm = TRUE)}) | ||
df3 <- melt(as.data.frame(df3), "group") | ||
colnames(df3) <- c("group", "publication_time", "relative") | ||
df3$publication_time <- as.numeric(as.character(df3$publication_time)) | ||
df3 <- df3[, c("publication_time", "group", "relative")] | ||
|
||
# Combine counts and relatives | ||
dfs <- dplyr::full_join(df2, df3) | ||
dfs$mode <- dfs[[mode]] | ||
|
||
return(dfs) | ||
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.