-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeframe.R
More file actions
48 lines (40 loc) · 1.08 KB
/
codeframe.R
File metadata and controls
48 lines (40 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require(dplyr)
#' Create a new codeframe object
#'
#' @examples
#' codeframe()
#' codeframe(covid=c("coronavirus"))
#' codeframe(covid=c("coronavirus"), jobs=c("job", "work"))
codeframe <- function(...) {
data <- list(...)
return(as.codeframe(data))
}
#' Convert a list of vectors into a codeframe.
#'
#' @examples
#' as.codeframe(list(covid=c("coronavirus")))
as.codeframe <- function(x) {
codeframe <- structure(as.list(x), class = "codeframe")
return(codeframe)
}
#' Read a codeframe from CSV.
#'
#' @examples
#' read.codeframe("data/example-codeframe.csv")
read.codeframe <- function(...) {
data <- read.csv(...) %>% unstack(keyword ~ topic)
return(as.codeframe(data))
}
union <- function(x, y) UseMethod("union", x)
union.default <- function(x, y) base::union(x, y)
#' Merge two codeframes, combining keywords for each topic.
#'
#' @examples
#' union(frame1, frame2)
union.codeframe <- function(x, y) {
frame <- rlang::duplicate(x)
for (topic in union(names(x), names(y))) {
frame[[topic]] <- union(x[[topic]], y[[topic]])
}
return(frame)
}