vector of character with unequal width
On Fri, Jan 07, 2011 at 02:55:18PM +0000, jose Bartolomei wrote:
Dear R users,
Thanks for your help
The recomendations were exaclty what I was searching.
Bellow the one I will use for my script.
Thanks again,
jose
nchar(xx)
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 4 4 4 4 4 4 4 4
[75] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
z1 <- rep("000000000", times=length(xx))
z2 <- substr(z1, 1, 9 - nchar(xx))
xx <- paste(z2, xx, sep="")
xx<-substring(xx, 6, 9)
nchar(xx)
[1] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
[38] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
[75] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
Let me suggest a slightly simpler code, which produces the same
output, if the input has length at most 9.
xx <- c("abc", "abcd", "abcde")
xx <- paste("000", xx, sep="")
xx <- substr(xx, nchar(xx) - 3, nchar(xx))
cbind(xx)
# xx
#[1,] "0abc"
#[2,] "abcd"
#[3,] "bcde"
Petr Savicky.