mirror of
https://github.com/agdamsbo/daDoctoR.git
synced 2024-11-24 04:31:54 +01:00
new function
This commit is contained in:
parent
3425590553
commit
b5152b5e70
29
R/replace_char.R
Normal file
29
R/replace_char.R
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#' Replacing specific characters or string elements with gsub()
|
||||||
|
#'
|
||||||
|
#' Using gsub() to make repeated character string element replacements.
|
||||||
|
#' @param char.vec Vector of elements with to replace.
|
||||||
|
#' @param to.be.rep Vector of characters to replace.
|
||||||
|
#' @param replacement Vector of characters to replace with.
|
||||||
|
#' @keywords replace
|
||||||
|
#' @export
|
||||||
|
#' @examples
|
||||||
|
#' n <- c("Se, en fraek raev", "Oesterland", "Aalborg", "Soester Aase", "Soendergaard")
|
||||||
|
#' p <- c('Ae', 'ae', 'Oe', 'oe','Aa','aa')
|
||||||
|
#' r <- c('Æ', 'æ', 'Ø', 'ø','Å','å')
|
||||||
|
#' replace_char(n,p,r)
|
||||||
|
|
||||||
|
replace_char<-function(char.vec,to.be.rep,replacement){
|
||||||
|
|
||||||
|
if (length(to.be.rep)!=length(replacement)) {stop("Replacement vectors are not the same length")}
|
||||||
|
if (!is.vector(char.vec)) {stop("The provided data should be a vector")}
|
||||||
|
|
||||||
|
n<-char.vec
|
||||||
|
for(i in seq_along(n)) {
|
||||||
|
s<-n[i]
|
||||||
|
for (j in seq_along(to.be.rep)){
|
||||||
|
s <- gsub(to.be.rep[j],replacement[j], s)
|
||||||
|
}
|
||||||
|
n[i]<-s
|
||||||
|
}
|
||||||
|
return(n)
|
||||||
|
}
|
25
man/replace_char.Rd
Normal file
25
man/replace_char.Rd
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
% Generated by roxygen2: do not edit by hand
|
||||||
|
% Please edit documentation in R/replace_char.R
|
||||||
|
\name{replace_char}
|
||||||
|
\alias{replace_char}
|
||||||
|
\title{Replacing specific characters or string elements with gsub()}
|
||||||
|
\usage{
|
||||||
|
replace_char(char.vec, to.be.rep, replacement)
|
||||||
|
}
|
||||||
|
\arguments{
|
||||||
|
\item{char.vec}{Vector of elements with to replace.}
|
||||||
|
|
||||||
|
\item{to.be.rep}{Vector of characters to replace.}
|
||||||
|
|
||||||
|
\item{replacement}{Vector of characters to replace with.}
|
||||||
|
}
|
||||||
|
\description{
|
||||||
|
Using gsub() to make repeated character string element replacements.
|
||||||
|
}
|
||||||
|
\examples{
|
||||||
|
n <- c("Se, en fraek raev", "Oesterland", "Aalborg", "Soester Aase", "Soendergaard")
|
||||||
|
p <- c('Ae', 'ae', 'Oe', 'oe','Aa','aa')
|
||||||
|
r <- c('Æ', 'æ', 'Ø', 'ø','Å','å')
|
||||||
|
replace_char(n,p,r)
|
||||||
|
}
|
||||||
|
\keyword{replace}
|
Loading…
Reference in New Issue
Block a user