Skip to content

vector of character with unequal width

5 messages · jose Bartolomei, Henrique Dallazuanna, Petr Savicky

#
On Wed, Jan 05, 2011 at 03:50:13PM +0000, jose Bartolomei wrote:
[...]
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
#
On Fri, Jan 07, 2011 at 02:55:18PM +0000, jose Bartolomei wrote:
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.