multigsub: Multiple gsub

Description Usage Arguments Value Note See Also Examples

View source: R/multigsub.R

Description

multigsub - A wrapper for gsub that takes a vector of search terms and a vector or single value of replacements.

sub_holder - This function holds the place for particular character values, allowing the user to manipulate the vector and then revert the place holders back to the original values.

Usage

1
2
3
4
5
6
7
8
multigsub(pattern, replacement, text.var, leadspace = FALSE,
  trailspace = FALSE, fixed = TRUE, trim = TRUE, order.pattern = fixed,
  ...)

mgsub(pattern, replacement, text.var, leadspace = FALSE, trailspace = FALSE,
  fixed = TRUE, trim = TRUE, order.pattern = fixed, ...)

sub_holder(pattern, text.var, alpha.type = TRUE, ...)

Arguments

pattern

Character string to be matched in the given character vector.

replacement

Character string equal in length to pattern or of length one which are a replacement for matched pattern.

text.var

The text variable.

leadspace

logical. If TRUE inserts a leading space in the replacements.

trailspace

logical. If TRUE inserts a trailing space in the replacements.

fixed

logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.

trim

logical. If TRUE leading and trailing white spaces are removed and multiple white spaces are reduced to a single white space.

order.pattern

logical. If TRUE and fixed = TRUE, the pattern string is sorted by number of characters to prevent substrings replacing meta strings (e.g., pattern = c("the", "then") resorts to search for "then" first).

...

Additional arguments passed to gsub.

alpha.type

logical. If TRUE alpha (lower case letters) are used for the key. If FALSE numbers are used as the key.

Value

multigsub - Returns a vector with the pattern replaced.

sub_holder - Returns a list with the following:

output

keyed place holder character vector

unhold

A function used to revert back to the original values

Note

The unhold function for sub_holder will only work on keys that have not been disturbed by subsequent alterations. The key follows the pattern of 'qdapplaceholder' followed by lower case letter keys followed by 'qdap'.

See Also

gsub

Examples

 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
## Not run: 
## ======================
##    `mgsub` Function
## ======================

multigsub(c("it's", "I'm"), c("it is", "I am"), DATA$state)
mgsub(c("it's", "I'm"), c("it is", "I am"), DATA$state)
mgsub("[[:punct:]]", "PUNC", DATA$state, fixed = FALSE)

## ====================== 
## `sub_holder` Function
## ======================

## `alpha.type` as TRUE
(fake_dat <- paste(emoticon[1:11,2], DATA$state))
(m <- sub_holder(emoticon[,2], fake_dat))
m$unhold(strip(m$output))
# With Stemming
m$unhold(stemmer(strip(m$output), capitalize = FALSE))

## `alpha.type` as FALSE (numeric keys)
vowels <- LETTERS[c(1, 5, 9, 15, 21)]
(m2 <- sub_holder(vowels, toupper(DATA$state), alpha.type = FALSE))
m2$unhold(gsub("[^0-9]", "", m2$output))
mtabulate(strsplit(m2$unhold(gsub("[^0-9]", "", m2$output)), ""))

## End(Not run)

Example output

Loading required package: qdapDictionaries
Loading required package: qdapRegex
Loading required package: qdapTools
Loading required package: RColorBrewer
OpenJDK 64-Bit Server VM warning: Can't detect initial thread stack location - find_vma failed

Attaching package: 'qdap'

The following object is masked from 'package:base':

    Filter

 [1] "Computer is fun. Not too fun."       
 [2] "No it is not, it is dumb."           
 [3] "What should we do?"                  
 [4] "You liar, it stinks!"                
 [5] "I am telling the truth!"             
 [6] "How can we be certain?"              
 [7] "There is no way."                    
 [8] "I distrust you."                     
 [9] "What are you talking about?"         
[10] "Shall we move on? Good then."        
[11] "I am hungry. Let's eat. You already?"
 [1] "Computer is fun. Not too fun."       
 [2] "No it is not, it is dumb."           
 [3] "What should we do?"                  
 [4] "You liar, it stinks!"                
 [5] "I am telling the truth!"             
 [6] "How can we be certain?"              
 [7] "There is no way."                    
 [8] "I distrust you."                     
 [9] "What are you talking about?"         
[10] "Shall we move on? Good then."        
[11] "I am hungry. Let's eat. You already?"
 [1] "Computer is funPUNC Not too funPUNC"               
 [2] "No itPUNCs notPUNC itPUNCs dumbPUNC"               
 [3] "What should we doPUNC"                             
 [4] "You liarPUNC it stinksPUNC"                        
 [5] "I am telling the truthPUNC"                        
 [6] "How can we be certainPUNC"                         
 [7] "There is no wayPUNC"                               
 [8] "I distrust youPUNC"                                
 [9] "What are you talking aboutPUNC"                    
[10] "Shall we move onPUNC Good thenPUNC"                
[11] "IPUNCm hungryPUNC LetPUNCs eatPUNC You alreadyPUNC"
 [1] "(.V.) Computer is fun. Not too fun."      
 [2] "O:-) No it's not, it's dumb."             
 [3] "X-( What should we do?"                   
 [4] "~:0 You liar, it stinks!"                 
 [5] ":-D I am telling the truth!"              
 [6] "(*v*) How can we be certain?"             
 [7] ":-# There is no way."                     
 [8] "</3 I distrust you."                      
 [9] "=^.^= What are you talking about?"        
[10] "*<:o) Shall we move on?  Good then."      
[11] "O.o I'm hungry.  Let's eat.  You already?"
 [1] "qdapplaceholderaqdap Computer is fun. Not too fun."      
 [2] "qdapplaceholderbqdap No it's not, it's dumb."            
 [3] "qdapplaceholdercqdap What should we do?"                 
 [4] "qdapplaceholderdqdap You liar, it stinks!"               
 [5] "qdapplaceholdereqdap I am telling the truth!"            
 [6] "qdapplaceholderfqdap How can we be certain?"             
 [7] "qdapplaceholdergqdap There is no way."                   
 [8] "qdapplaceholderhqdap I distrust you."                    
 [9] "qdapplaceholderiqdap What are you talking about?"        
[10] "qdapplaceholderjqdap Shall we move on? Good then."       
[11] "qdapplaceholderkqdap I'm hungry. Let's eat. You already?"
 [1] "(.V.) computer is fun not too fun"  "O:-) no its not its dumb"          
 [3] "X-( what should we do"              "~:0 you liar it stinks"            
 [5] ":-D i am telling the truth"         "(*v*) how can we be certain"       
 [7] ":-# there is no way"                "</3 i distrust you"                
 [9] "=^.^= what are you talking about"   "*<:o) shall we move on good then"  
[11] "O.o im hungry lets eat you already"
 [1] "(.V.) comput is fun not too fun"   "O:-) no it not it dumb"           
 [3] "X-( what should we do"             "~:0 you liar it stink"            
 [5] ":-D i am tell the truth"           "(*v*) how can we be certain"      
 [7] ":-# there is no way"               "</3 i distrust you"               
 [9] "=^.^= what are you talk about"     "*<:o) shall we move on good then" 
[11] "O.o im hungri let eat you alreadi"
Warning message:
In stemmer(strip(m$output), capitalize = FALSE) :
  The following row(s) do have standard qdap punctuation endmarks:
  rows: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 

 [1] "C4MP5T2R 3S F5N. N4T T44 F5N."       "N4 3T'S N4T, 3T'S D5MB."            
 [3] "WH1T SH45LD W2 D4?"                  "Y45 L31R, 3T ST3NKS!"               
 [5] "3 1M T2LL3NG TH2 TR5TH!"             "H4W C1N W2 B2 C2RT13N?"             
 [7] "TH2R2 3S N4 W1Y."                    "3 D3STR5ST Y45."                    
 [9] "WH1T 1R2 Y45 T1LK3NG 1B45T?"         "SH1LL W2 M4V2 4N? G44D TH2N."       
[11] "3'M H5NGRY. L2T'S 21T. Y45 1LR21DY?"
 [1] "OUEIUOOOU"  "OIOIU"      "AOUEO"      "OUIAII"     "IAEIEU"    
 [6] "OAEEEAI"    "EEIOA"      "IIUOU"      "AAEOUAIAOU" "AEOEOOOE"  
[11] "IUEEAOUAEA"
   A E I O U
1  0 1 1 4 3
2  0 0 2 2 1
3  1 1 0 2 1
4  1 0 3 1 1
5  1 2 2 0 1
6  2 3 1 1 0
7  1 2 1 1 0
8  0 0 2 1 2
9  4 1 1 2 2
10 1 3 0 4 0
11 3 3 1 1 2
Warning message:
system call failed: Cannot allocate memory 

qdap documentation built on May 15, 2018, 9:04 a.m.