3

I have a data frame make like this:

data.names<-data.frame(DATA=c(1:5))
rownames(data.names)<-c("IV\xc1N","JOS\xc9","LUC\xcdA","RAM\xd3N","TO\xd1O")
data.names
#          DATA
# IV\xc1N     1
# JOS\xc9     2
# LUC\xcdA    3
# RAM\xd3N    4
# TO\xd1O     5

I want the incorrect letters replace by the right ones (Á,É,Í,...). Make clear that I want to use apply because I read that is much more efficient apply than for. My idea is make a function that changes these letters:

letters1<-c("\xc1","\xc9","\xcd","\xd3", "\xd1") #Á,É,Í,Ó,Ñ
letters2<-c("Á","É","Í","Ó","Ñ")
change.names <- function(x){sub(letters1[x], letters2[x],rownames(data.names))}

Now, with a for I haven't any problems:

for(i in 1:5) rownames(data.names)<-change.names(i)
data.names
#       DATA
# IVÁN     1
# JOSÉ     2
# LUCÍA    3
# RAMÓN    4
# TOÑO     5

But I don't have much idea how to do it with apply. I've tried:

apply(matrix(c(1:5),ncol=5),2,change.names)

And the output is a matrix with 5 columns, where each one only changes one letter and I can't know how to assign to rownames(data.names) a "mix" of them, or something that works.

jopasserat
  • 5,721
  • 4
  • 31
  • 50
DonDraper
  • 31
  • 1
  • 3
  • 1
    rownames(data.names)<-sub(letters1, letters2,rownames(data.names)) seems to work for me with no need for a loop – Zfunk Dec 16 '15 at 16:41

1 Answers1

4

You don't even need to use apply, because rownames(data.names) is a vector and vectors may be recycled

> Encoding(rownames(data.names)) <- 'latin1'
> data.names
         DATA
IVÁN        1
JOSÉ        2
LUCÍA       3
RAMÓN       4
TOÑO        5

Please read this answer for more details about the encoding.

Community
  • 1
  • 1
iled
  • 2,142
  • 3
  • 31
  • 43