-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdifs.R
39 lines (35 loc) · 1.09 KB
/
difs.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
#====================================================================#
# Author: Damian Gwozdz (DG)
# Function: difs
# Creation date: 31AUG2017
# Last modified: -
# Description: Function to add differences of selected variables
# to the given dataset
# Required functions: -
#
#====================================================================#
difs <- function(dset, vars, dif.vec){
#====================================================================
# PARAMETERS:
#
# 1) dset - input data set
# 2) vars - vector of variables names
# 3) dif.vec - vector of differences
#
# EXAMPLE:
#
# iris2 <- difs(iris, c("Sepal.Length"), c(3, 5, 7))
#
#====================================================================
## Parameters for tests
# dset <- iris
# vars <- c("Sepal.Length")
# dif.vec <- c(3,5)
for(i in 1:length(vars)){
for(j in 1:length(dif.vec)){
dset[[paste0("Diff.", vars[i], ".", dif.vec[j])]] <-
c(rep(NA, dif.vec[j]), diff(dset[[vars[i]]], dif.vec[j]))
}
}
return(dset)
}