I have vector of characters and I want to make sure all elements of the vector have the same length. Hence I fill short elements up with spaces, like this:
vec <- c("fjdlksa01dada","rau","sjklf")
x <- sprintf("%-15s", vec)
nchar(x)
# returns
[1] 15 15 15
like answers to my previous question suggested. This is fine but it seems to have trouble with umlauts. For example if my vector looks like this:
vec2 <- c("fjdlksa01dada","rauü","sjklf")
y <- sprintf("%-15s", vec)
nchar(y)
# returns
[1] 15 14 15
I am running R on Mac OS X (10.6). How can I fix this?
EDIT: Note, I am not looking to fix the output of nchar because it is correct. The problem is that sprintf looses the umlaut.
EDIT: Update R, changed to DWins locale - no change at all. But:
vec2 <- c("fjdlksa01dada","rauü","sjklf")
Encoding(vec2)
# returns
[1] "unknown" "UTF-8" "unknown"
strange.