[R] vector of character with unequal width

Petr Savicky savicky at cs.cas.cz
Wed Jan 5 19:50:28 CET 2011


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.



More information about the R-help mailing list