-
Notifications
You must be signed in to change notification settings - Fork 0
/
catching functions.R
156 lines (136 loc) · 4.51 KB
/
catching functions.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
###########################################
###########################################
########### ############
########### Catching Functions ############
########### ############
###########################################
###########################################
#############################
###:: NGrams ::###
#############################
#############################################
getgram <- function(x, n = 2) {
#:: x should be a string "XXXX"
#:: n represents the gramme size
x <- unlist(strsplit(x, ""))
base <- x
temp <- x
for(i in 1 : (n - 1) ){
temp <- dplyr::lead(temp)
base <- cbind(base, temp)
}
base <- na.omit(base)
out <- apply(base, 1, paste, collapse = "")
#out <- data.frame(Grams = names(table(out)),
#Counts = as.vector(table(out)))
return(out)
}
############################################
gram2table <- function(x) {
#:: x is obtained by the getgram fucntion
l <- length(x)
base <- x[[1]]
for (i in 2 : l) {
base <- dplyr::full_join(base, x[[i]], by = "Grams")
}
Names <- base[, 1]
out <- t(base)[-1, ]
colnames(out) <- Names
rownames(out) <- NULL
return(as.data.frame(out))
}
###############################################
getpermn <- function(x, n) {
temp <- x
for(i in 1 : (n - 1)) {
temp <- apply(expand.grid(temp, x), 1, paste, collapse = "")
}
return(temp)
}
##############################################
getpermnbyN <- function(x, n) {
#: x must be the unique case
l <- length(x)
if (n <= l) {
combos <- combn(x, n)
out <- apply(combos, 2, getpermn, n = n)
} else {print("Error: n need to be <= length of x")}
out <- unique(as.vector(out))
return(out)
}
#############################################
getGramTable <- function(x, perm) {
#: x should be obtained from getgram
#: perm is a vector obtained by getpermbyN
table <- t(as.matrix(rep(0, length(perm))))
colnames(table) <- perm
for (i in 1 : length(x)) {
table[which(x[i] == perm)] <- table[which(x[i] == perm)] + 1
}
return(table)
}
###########################################
###:: Higher-level Action Sequences ::###
###########################################
highlevel <- function(x) {
#:: split the sequence into actions
temp <- unlist(strsplit(as.character(x), ", "))
#:: subset out the first letter of each action
temp <- substr(temp, 1, 1)
#:: get the highlevel sequence without "START" and "END"
sequence <- paste(temp, collapse = "")
#sequence <- paste(temp[-c(1, length(temp))], collapse = "")
#:: counting the number of each higher level action
# Rcounts <- sum(temp == "R")
# Acounts <- sum(temp == "A")
# Dcounts <- sum(temp == "D")
return(sequence)
# return(list("R" = Rcounts, "A" = Acounts, "D" = Dcounts))
}
highlevel.check <- function(x) {
#:: split the sequence into actions
temp <- unlist(strsplit(as.character(x), ", "))
#:: subset out the first letter of each action
temp <- substr(temp, 1, 1)
#:: get the highlevel sequence without "START" and "END"
sequence <- paste(temp[c(1,length(temp))], collapse = "")
#:: counting the number of each higher level action
# Rcounts <- sum(temp == "R")
# Acounts <- sum(temp == "A")
# Dcounts <- sum(temp == "D")
return(sequence)
# return(list("R" = Rcounts, "A" = Acounts, "D" = Dcounts))
}
###########################################
###:: obtain VOTAT in Sequence Action ::###
###########################################
vary_one <- function(x){
temp <- unlist(strsplit(x[1], "")) == unlist(strsplit(x[2], ""))
counts <- sum(temp)
if(counts == 2) {
if (temp[1] == F) {Vposition <- "T"}
if (temp[2] == F) {Vposition <- "C"}
if (temp[3] == F) {Vposition <- "B"}
} else {Vposition <- "N"}
return(Vposition)
}
getVOTAT <- function(x) {
temp <- unlist(strsplit(x, ", "))
#:: get the action with type "APPLY"
temp <- temp[which(substr(temp, 1, 1) == "A" | substr(temp, 1, 1) == "R")]
#:: obtain the last 3 letters of this action
if (length(temp) == 0) {
temp <- "N"
} else if (length(temp) == 1) {
temp <- "N"
} else {
temp <- substr(temp, 2, 4)
tempMatrix <- cbind(temp[1 : (length(temp) - 1)], temp[2 : length(temp)])
temp <- paste(apply(tempMatrix, 1, vary_one), collapse = "")
}
return(temp)
}
#############################
###:: obtain Hesitation ::###
#############################
hesitation