-
Notifications
You must be signed in to change notification settings - Fork 1
/
tbl_replace.R
163 lines (147 loc) · 5.59 KB
/
tbl_replace.R
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#' Replace numeric values less than given \code{'cutoff'}
#'
#' Replace numeric values less than given \code{'cutoff'} from a tbl.
#'
#' @param tbl a tbl.
#' @param cutoff a numeric value to be used as a cutoff.
#' @param replace_by a numeric value to be used to replace less than \code{cutoff}.
#'
#' @return a tbl.
#' @export
#'
#' @examples
#' \dontrun{
#' tbl <- tibble::tibble(x = letters[1:5] , y = LETTERS[1:5] , z = 1:5 , w = 5:1)
#' tbl %>% tbl_replace_less_than(4, 0 )
#' }
#'
tbl_replace_less_than <- function(tbl, cutoff, replace_by ){
if ( !is_tibble(tbl) ) stop("tbl is not tbl")
mm <- purrr::as_mapper(~ ..1 %>% mutate_if(is.numeric , ~ if_else(. < !!..2 , !!..3 , as.double(.) ) ))
tbl %>% mm(cutoff, replace_by)
}
#' Replace numeric values less than or equal given '\code{'cutoff'}
#'
#' Replace numeric values less than or equal given '\code{'cutoff'} from a tbl.
#'
#' @param tbl a tbl.
#' @param cutoff a numeric value to be used as a \code{'cutoff'}.
#' @param replace_by a numeric value to be used to replace less than or equal \code{cutoff}
#'
#' @return a tbl.
#' @export
#'
#' @examples
#' \dontrun{
#' tbl <- tibble::tibble(x = letters[1:5] , y = LETTERS[1:5] , z = 1:5 , w = 5:1)
#' tbl %>% tbl_replace_less_than_or_equal(4, 0 )
#' }
#'
tbl_replace_less_than_or_equal <- function(tbl, cutoff, replace_by ){
if ( !is_tibble(tbl) ) stop("tbl is not tbl")
mm <- purrr::as_mapper(~ ..1 %>% mutate_if(is.numeric , ~ if_else(. <= !!..2 , !!..3 , as.double(.) ) ))
tbl %>% mm(cutoff, replace_by)
}
#' Replace numeric values greater than given \code{'cutoff'}
#'
#' Replace numeric values less than or equal given \code{'cutoff'} from a tbl.
#'
#' @param tbl a tbl.
#' @param cutoff a numeric value to be used as a \code{'cutoff'}.
#' @param replace_by a numeric value to be used to replace greater than \code{'cutoff'}.
#'
#' @return a tbl.
#' @export
#'
#' @examples
#' \dontrun{
#' tbl <- tibble::tibble(x = letters[1:5] , y = LETTERS[1:5] , z = 1:5 , w = 5:1)
#' tbl %>% tbl_replace_greater_than(2, 1000 )
#' }
#'
tbl_replace_greater_than <- function(tbl, cutoff, replace_by){
if ( !is_tibble(tbl) ) stop("tbl is not tbl")
mm <- purrr::as_mapper(~ ..1 %>% mutate_if(is.numeric , ~ if_else(. > !!..2 , !!..3 , as.double(.) ) ))
tbl %>% mm(cutoff , replace_by)
}
#' Replace numeric values greater than or equal to given \code{'cutoff'}
#'
#' Replace numeric values greater than or equal to given \code{'cutoff'} from a tbl.
#'
#' @param tbl a tbl.
#' @param cutoff numeric value to be used as a \code{'cutoff'}.
#' @param replace_by a numeric value to be used to replace greater than or equal \code{'cutoff'}.
#' @importFrom purrr as_mapper
#' @importFrom dplyr mutate_if
#' @return a tbl.
#' @export
#'
#' @examples
#' \dontrun{
#' tbl <- tibble::tibble(x = letters[1:5] , y = LETTERS[1:5] , z = 1:5 , w = 5:1)
#' tbl %>% tbl_replace_greater_than_or_equal(2, 1000 )
#' }#'
#'
#'
tbl_replace_greater_than_or_equal <- function(tbl, cutoff, replace_by){
if ( !is_tibble(tbl) ) stop("tbl is not tbl")
mm <- purrr::as_mapper(~ ..1 %>% dplyr::mutate_if(is.numeric , ~ if_else(. >= !!..2 , !!..3 , as.double(.) ) ))
tbl %>% mm(cutoff , replace_by)
}
#' Wrapper around \code{stringr::str_replace_all()}
#'
#' Replace all occurance of \code{'pattern'} in all non numeric columns from a tbl.
#'
#' @param tbl a tbl.
#' @param pattern pattern to look for. Will be passed to argument \code{pattern} to function \code{stringr::str_replace_all()}.
#' @param replacement a character vector of replacements. Will be passed to argument \code{replacement} to function \code{stringr::str_replace_all()}.
#'
#' @return a tbl.
#' @importFrom purrr as_mapper
#' @importFrom dplyr mutate_if
#' @importFrom stringr str_replace_all
#' @export
#' @examples
#'
#' \dontrun{
#' tbl <- tibble::tibble(x = c(letters[1:5] ,letters[1:5] ) ,
#' y = c(LETTERS[1:5],LETTERS[1:5]) , z = 1:10 )
#' tbl %>% tbl_replace_string(pattern = "a" , replacement = "a_changed")
#' tbl %>% tbl_replace_string("[aeiou]", toupper)
#' tbl %>% tbl_replace_string("([aeiou])", "")
#' tbl %>% tbl_replace_string(c("a", "e", "i"), "-")
#' tbl %>% tbl_replace_string("b", NA_character_)
#'
#' }
#' @seealso stringr::str_replace_all()
#'
tbl_replace_string <- function(tbl, pattern, replacement){
if ( !is_tibble(tbl) ) stop("tbl is not tbl")
mm <- purrr::as_mapper(~ ..1 %>% dplyr::mutate_if(is.character, ~ stringr::str_replace_all( . ,pattern = !!..2 ,
replacement = !!..3)))
tbl %>% mm(pattern , replacement)
}
#' @title Replace numeric value in a tibble.
#' @description In a given tibble a numeric value can be replaced by another numeric value.
#' @param tbl a tbl.
#' @param value a numeric which is to be replaced from \code{tbl}.
#' @param replace_by a numeric replacement for \code{value}.
#' @return a tbl
#' @export
#' @examples
#' \dontrun{
#' set.seed(1234)
#' tbl <- tibble::tibble(x = c(letters[1:5] ,letters[1:5]),
#' y = sample(1:5, 10 ,replace = T),
#' z = sample(1:5, 10,replace = T))
#' tbl %>% tbl_replace_numeric(2, 1000)
#' tbl %>% tbl_replace_numeric(4, 1000)
#' }
#'
tbl_replace_numeric <- function(tbl, value , replace_by){
if ( !is_tibble(tbl) ) stop("tbl is not tbl")
stopifnot(is.numeric(value))
stopifnot(is.numeric(replace_by))
mm <- purrr::as_mapper(~ ..1 %>% dplyr::mutate_if(is.numeric , ~ dplyr::if_else(. == !!..2 , !!..3 , as.double(.) )))
tbl %>% mm(value , replace_by)
}