-
Notifications
You must be signed in to change notification settings - Fork 114
/
charutils.r
192 lines (139 loc) · 9.07 KB
/
charutils.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
#==========================================================================================#
#==========================================================================================#
# This sub-routine makes the first letter of every entry capitalised, whilst leaving #
# the other letters lower case. #
# Original subroutine comes from the help on toupper. Here I modified it just to deal #
# with NAs. #
#------------------------------------------------------------------------------------------#
capwords <<- function(s, strict = FALSE) {
#----- Function to be applied for each element of s. ----------------------------------#
cap = function(x,strict=FALSE){
#----- First letter is always upper case. -----------------------------------------#
first = toupper(substring(x,1,1))
#----- Check whether to force the remainder to be lower case or not. --------------#
if (strict){
remainder = tolower(substring(x,2))
}else{
remainder = substring(x,2)
}#end if
#----------------------------------------------------------------------------------#
ans = paste(first,remainder,sep="",collapse=" ")
return(ans)
}#end function
#--------------------------------------------------------------------------------------#
#----- Remember which elements were NA, then we reset them to NA. ---------------------#
sel = is.na(s)
#--------------------------------------------------------------------------------------#
#----- Fix case for all dataset. ------------------------------------------------------#
ans = sapply( X=strsplit(s, split = " "),FUN=cap,strict=strict
, USE.NAMES = !is.null(names(s)))
#--------------------------------------------------------------------------------------#
#---- Force NAs to remain NAs. --------------------------------------------------------#
ans[sel] = NA_character_
return(ans)
#--------------------------------------------------------------------------------------#
}#end if
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
# This function deletes spaces from strings, trimming. Default is to trim both #
# sides, but you can also trim the left or the right only. #
#------------------------------------------------------------------------------------------#
trim <<- function(x,side="both"){
if (side %in% c("both","left") ) x = sub(pattern="^\\s+",replacement="",x=x)
if (side %in% c("both","right")) x = sub(pattern="\\s+$",replacement="",x=x)
return(x)
}#end function trim
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
# This function concatenates two strings, but skips the NAs. #
#------------------------------------------------------------------------------------------#
concatenate.message <<- function(x1,x2,sep="; "){
if (length(x1) != length(x2)) stop(" Message vectors must have the same length!")
only.x1 = ( ! is.na(x1) ) & is.na(x2)
only.x2 = is.na(x1) & ( ! is.na(x2) )
both = ( ! is.na(x1) ) & ( ! is.na(x2) )
full = rep(NA_character_,times=length(x1))
full[only.x1] = x1[only.x1]
full[only.x2] = x2[only.x2]
full[both ] = paste(x1[both],x2[both],sep=sep)
return(full)
}#end function
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
# This function pastes consecutive elements. x is the vector with the elements to #
# be pasted, and ... are additional options for paste. #
#------------------------------------------------------------------------------------------#
pair.paste <<- function(x,...) apply(X=cbind(x[-length(x)],x[-1]),MARGIN=1,FUN=paste,...)
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
# This function is similar to paste0, but for printing stuff on screen. #
#------------------------------------------------------------------------------------------#
cat0 <<- function(...,file="",fill=FALSE,labels=NULL,append=FALSE){
cat(...,"\n",file=file,fill=fill,sep="",labels=labels,append=append)
}#end cat0
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
# This function is similar to Fortran adjust left. #
#------------------------------------------------------------------------------------------#
adjust.left <<- function(x,width=NULL){
if (is.null(width)) width = max(nchar(x))
names.x = names(x)
out = sapply( X = x
, FUN = function(x,w){
ans = rep(" ",times=w)
ans[sequence(nchar(x))] = unlist(strsplit(x=x,split=""))
ans = paste(ans,collapse="")
return(ans)
}#end FUN
, w = width
)#end sapply
names(out) = names.x
return(out)
}#end adjust.left
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
# This function is similar to Fortran adjust right. #
#------------------------------------------------------------------------------------------#
adjust.right <<- function(x,width){
if (is.null(width)) width = max(nchar(x))
fmt = paste0("%",width,"s")
out = sprintf(fmt,x)
names(out) = names(x)
return(out)
}#end adjust.right
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
#==========================================================================================#
# This function is similar to gregexpr, but it returns the number of occurrences of #
# pattern. #
#------------------------------------------------------------------------------------------#
nregexpr <<- function(pattern,text,...){
if (length(text) > 1){
ans = mapply(FUN=nregexpr,text=text,MoreArgs=list(pattern=pattern),SIMPLIFY=TRUE)
}else{
#---- Use gregexpr to identifiy the number of occurrences. --------------------------#
ans = gregexpr(pattern=pattern,text=text,...)[[1]]
ans = length(ans[ans %gt% 0])
#------------------------------------------------------------------------------------#
}#end if (length(text) > 1)
#---------------------------------------------------------------------------------------#
#----- Answer. -------------------------------------------------------------------------#
names(ans) = names(text)
return(ans)
#---------------------------------------------------------------------------------------#
}#end function nregexpr
#==========================================================================================#
#==========================================================================================#