-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathadd-var.R
273 lines (225 loc) · 8.02 KB
/
add-var.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#' Add a random categorical variable to dataset
#'
#' @param data A dataset
#' @param name Name of new variable (as string)
#' @param cat Vector of categories
#' @param prob Vector of probabilities
#' @param overwrite Can new random variable overwrite an existing variable in dataset?
#' @param seed Seed for random number generation (integer)
#' @return Dataset containing new random variable
#' @examples
#' library(magrittr)
#' iris %>% add_var_random_cat() %>% head()
#' iris %>% add_var_random_cat(name = "my_cat") %>% head()
#' iris %>% add_var_random_cat(cat = c("Version A", "Version B")) %>% head()
#' iris %>% add_var_random_cat(cat = c(1,2,3,4,5)) %>% head()
#' @export
add_var_random_cat <- function(data,
name = "random_cat",
cat = LETTERS[1:6],
prob,
overwrite = TRUE,
seed) {
# data table available?
check_data_frame_non_empty(data)
# check if var already exists
if (name %in% names(data) & !overwrite) {
stop("Variable ", name, " already exists!")
}
# check prob
if (missing(prob)) {
prob <- rep(1, length(cat))
}
diff <- length(cat) - length(prob)
if (diff > 0) {
prob <- c(prob, rep(0, diff))
}
# check seed
if (!missing(seed)) {
set.seed(seed)
}
# create var
data[[name]] <- sample(cat,
size = nrow(data),
prob = prob,
replace = TRUE)
# return data
data
} # add_var_random_cat
#' Add a random 0/1 variable to dataset
#'
#' @param data A dataset
#' @param name Name of new variable (as string)
#' @param prob Vector of probabilities
#' @param overwrite Can new random variable overwrite an existing variable in dataset?
#' @param seed Seed for random number generation (integer)
#' @return Dataset containing new random variable
#' @examples
#' library(magrittr)
#' iris %>% add_var_random_01() %>% head()
#' iris %>% add_var_random_01(name = "my_var") %>% head()
#' @export
add_var_random_01 <- function(data, name = "random_01", prob = c(0.5, 0.5), overwrite = TRUE, seed) {
data <- add_var_random_cat(data,
name = name,
cat = c(0L, 1L),
prob = prob,
overwrite = overwrite,
seed = seed)
data
} # add_var_random_01
#' Add a random integer variable to dataset
#'
#' @param data A dataset
#' @param name Name of new variable (as string)
#' @param min_val Minimum random integers
#' @param max_val Maximum random integers
#' @param overwrite Can new random variable overwrite an existing variable in dataset?
#' @param seed Seed for random number generation (integer)
#' @return Dataset containing new random variable
#' @examples
#' library(magrittr)
#' iris %>% add_var_random_int() %>% head()
#' iris %>% add_var_random_int(name = "random_var") %>% head()
#' iris %>% add_var_random_int(min_val = 1, max_val = 10) %>% head()
#' @export
add_var_random_int <- function(data, name = "random_int",
min_val = 1, max_val = 10,
overwrite = TRUE,
seed) {
# data table available?
check_data_frame_non_empty(data)
if (name %in% names(data) & !overwrite) {
stop("Variable ", name, " already exists!")
}
# check seed
if (!missing(seed)) {
set.seed(seed)
}
# possible integer values
values <- seq(from = min_val, to = max_val)
# add random variable
data[[name]] <- sample(values, nrow(data), replace = TRUE)
# return data
data
} # add_var_random_int
#' Add a random double variable to dataset
#'
#' @param data A dataset
#' @param name Name of new variable (as string)
#' @param min_val Minimum random integers
#' @param max_val Maximum random integers
#' @param overwrite Can new random variable overwrite an existing variable in dataset?
#' @param seed Seed for random number generation (integer)
#' @return Dataset containing new random variable
#' @examples
#' library(magrittr)
#' iris %>% add_var_random_dbl() %>% head()
#' iris %>% add_var_random_dbl(name = "random_var") %>% head()
#' iris %>% add_var_random_dbl(min_val = 1, max_val = 10) %>% head()
#' @export
add_var_random_dbl <- function(data, name = "random_dbl",
min_val = 0, max_val = 100,
overwrite = TRUE,
seed) {
# data table available?
check_data_frame_non_empty(data)
# check variable name
if (name %in% names(data) & !overwrite) {
stop("Variable ", name, " already exists!")
}
# check seed
if (!missing(seed)) {
set.seed(seed)
}
# add random variable
data[[name]] <- stats::runif(nrow(data), min = min_val, max = max_val)
# return data
data
} # add_var_random_dbl
#' Add a random moon variable to dataset
#'
#' @param data A dataset
#' @param name Name of new variable (as string)
#' @param overwrite Can new random variable overwrite an existing variable in dataset?
#' @param seed Seed for random number generation (integer)
#' @return Dataset containing new random variable
#' @examples
#' library(magrittr)
#' iris %>% add_var_random_moon() %>% head()
#' @export
add_var_random_moon <- function(data, name = "random_moon", overwrite = TRUE, seed) {
# add starsign
moons <- c("New ( )", "Waxing (+)", "Full (O)", "Waning (-)")
data <- add_var_random_cat(data,
cat = moons,
name = name,
prob = c(0.1, 0.4, 0.1, 0.4),
overwrite = overwrite,
seed = seed)
# return data
data
} # add_var_random_moon
#' Add a random starsign variable to dataset
#'
#' @param data A dataset
#' @param name Name of new variable (as string)
#' @param lang Language used for starsign (en = English, de = Deutsch, es = Espanol)
#' @param overwrite Can new random variable overwrite an existing variable in dataset?
#' @param seed Seed for random number generation (integer)
#' @return Dataset containing new random variable
#' @examples
#' library(magrittr)
#' iris %>% add_var_random_starsign() %>% head()
#' iris %>% add_var_random_starsign(lang = "de") %>% head()
#' @export
add_var_random_starsign <- function(data, name = "random_starsign", lang = "en", overwrite = TRUE, seed) {
# add starsign
signs <- c("Aries", "Taurus", "Gemini", "Cancer", "Leo",
"Virgo", "Libra", "Scorpio", "Saggitarius",
"Carpicorn", "Aquarius", "Pisces")
if (lang == "de") {
signs <- c("Widder", "Stier", "Zwilling", "Krebs", "Loewe",
"Jungfrau", "Waage", "Skorpion", "Schuetze",
"Steinbock", "Wassermann","Fische")
}
if (lang == "es") {
signs <- c("Aries", "Tauro", "Geminis", "Cancer", "Leo",
"Virgo", "Libra", "Escorpio", "Sagitario",
"Capricornio", "Acuario", "Piscis")
}
data <- add_var_random_cat(data,
cat = signs,
name = name,
overwrite = overwrite,
seed = seed)
# return data
data
} # add_var_random_starsign
#' Add a variable id at first column in dataset
#'
#' @param data A dataset
#' @param name Name of new variable (as string)
#' @param overwrite Can new id variable overwrite an existing variable in dataset?
#' @return Data set containing new id variable
#' @examples
#' library(magrittr)
#' iris %>% add_var_id() %>% head()
#' iris %>% add_var_id(name = "iris_nr") %>% head()
#' @export
add_var_id <- function(data, name = "id", overwrite = FALSE) {
# data table available?
check_data_frame(data)
# check variable name
if (name %in% names(data) & !overwrite) {
stop("Variable ", name, " already exists!")
}
# drop var if overwrite == TRUE
if (name %in% names(data) & overwrite) {
data[[name]] <- NULL
}
# create id
data <- data.frame(seq(from = 1, to = nrow(data)), data)
names(data)[1] <- name
data
} # add_var_id