Hi everybody

 

I guess my problem is pretty easy to solve, but I didn't manage to get it
working...

 

Me, as a Swiss, I'm trying to substitute German "Umlauts" (ä, ü, ö) in a
string variable in a dataset; meaning "ä" to "ae", "ü", to "ue" and "ö" to
"oe".

Here's a reproducible example:

 

# defining dataset

d.data <- as.data.frame(cbind(c("äzÄzüzÜZöZÖ"), c(rnorm(1))))

# defining "Umlauts"

 

a <- c("ä", "ae")

A <- c("Ä", "Ae")

u <- c("ü", "ue")

U <- c("Ü", "Ue")

o <- c("ö", "oe")

O <- c("Ö", "Oe")

 

# Matrix of substitutions

 

m <- cbind(a,A,u,U,o,O, deparse.level=0)

 

I’m trying to write a function that does replace the "Umlauts" by the
appropriate substitute. Here's what I have so far:

 

swiss1 <- function(m, data){

  gsub(paste(m[,1], sep=",")[1],paste(m[,1], sep=",")[2], data,
ignore.case=T, perl = F, fixed = F, useBytes = F)

}

 

test1 <- swiss1(m, d.data$V1)

test1

> test

[1] "aezaezüzÜZöZÖ"

 

Apparently this does substitute the first elements of my matrix m. But i
want it to walk through the whole matrix m. So I thought of doing a for loop
inside the function.

 

swiss2 <- function(m, data){

  for (i in ncol(m)){

      gsub(paste(m[,i], sep=",")[1],paste(m[,i], sep=",")[2], data,
ignore.case=T, perl = F, fixed = F, useBytes = F)

  }

}

 

 

test2<- swiss2(m, d.data$V1)

test2

> test2

NULL

 

What I get then is an empty vector, instead of the original data with the
substituted "Umlauts"

 

Does anybody has a hint..?

 

Thanx very much!

Best, Tobi

 

 


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to