-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.R
More file actions
41 lines (37 loc) · 1.09 KB
/
util.R
File metadata and controls
41 lines (37 loc) · 1.09 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
require(dplyr)
require(stringr)
#' Clean responses for case and punctuation then split into words.
#'
#' Any punctuation is considered a word split except apostrophes.
#'
#' @examples
#' df$responses <- clean_responses(df$responses)
clean_responses <- function(responses) {
cleaned <- responses %>%
lapply(tolower) %>%
str_replace_all("[,.;:-]+", " ") %>% # Replace punctuation with spaces
str_remove_all("['’]+") %>% # Remove apostrophes - i.e. don't -> dont
strsplit(" +")
return(cleaned)
}
#' Write a list of responses to file without CSV decoration.
#'
#' @param responses List of responses
#' @param filepath Path of file to write
write_responses <- function(responses, filepath) {
if (file.exists(filepath)) {
file.remove(filepath)
}
lapply(responses, write, filepath, append = TRUE)
}
#' Duplicate rows in a dataframe.
#'
#' Useful for testing performance with bigger datasets.
#' @param frame Dataframe to double
#' @param doubles Number of doublings
double_frame <- function(frame, doubles) {
for (i in 1:doubles) {
frame <- rbind(frame, frame)
}
return(frame)
}