-
Notifications
You must be signed in to change notification settings - Fork 1
/
minhasfuncoes.R
51 lines (46 loc) · 1.07 KB
/
minhasfuncoes.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
# Carrega vários arquivos CSV numa mesma pasta
carrega <- function(extensao = "csv", sep = ";"){
require(readr)
arqs <- list.files(pattern = extensao)
for(i in arqs){
assign(i, read_delim(i, delim = sep, escape_double = FALSE, col_names = FALSE, trim_ws = TRUE), envir = .GlobalEnv)
}
}
#' Transforma uma série mensal pra trimestral.
#'
#' @param serie objeto do tipo `ts`
#' @param funcao pode ser `last`, `mean`, `cumsum`...
#'
#' @return
#' @export
#'
#' @examples
ts.trans <- function(serie, freq = 4, funcao){
aggregate(serie, nfrequency = freq, FUN = funcao)
}
#' Calcula a variação percentual da série de tempo.
#'
#' @param x objeto do tipo `ts`
#'
#' @return
#' @export
#'
#' @examples
percentual <- function(x){
y <- x/stats::lag(x,-1) - 1
y <- y*100
y
}
#' Gera um índice base 100
#'
#' @param x objeto do tipo `ts`
#' @param base um vetor `integer` que indica quantas observações na base
#'
#' @return
#' @export
#'
#' @examples
indice <- function(x, base=1){
y <- x/mean(x[base], na.rm = T)*100
y
}