An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110105/567c64ce/attachment.pl>
vector of character with unequal width
5 messages · jose Bartolomei, Henrique Dallazuanna, Petr Savicky
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110105/c155cc2a/attachment.pl>
On Wed, Jan 05, 2011 at 03:50:13PM +0000, jose Bartolomei wrote:
[...]
I was thinking to create a character vector of 0's 9-nchar(xx). Then paste it to xx. 9-nchar(xx) [1] 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 [38] 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 6 6 6 6 6 5 5 5 5 5 5 5 5 [75] 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 ......1 Nevertheless, I have not been able to create this vector nor I do not know if this is the best option.
Did you consider something like the following?
xx <- c("abc", "abcd", "abcde")
z1 <- rep("000000000", times=length(xx))
z2 <- substr(z1, 1, 9 - nchar(xx))
yy <- paste(z2, xx, sep="")
cbind(yy)
# yy
#[1,] "000000abc"
#[2,] "00000abcd"
#[3,] "0000abcde"
Petr Savicky.
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110107/7288460a/attachment.pl>
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.