Dear R users,
The best in this new year 2011.
I am dealing with a character vector (xx) whose nchar are not the same.
Ex.
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 ....... 9
I need xx to be nchar = 9
My best guest was to paste “0's”. Then I need substring (xx, 6, 9).
I came with:
xx[1:61]<-paste("00000000", xx[1:61], sep="")
xx[62:66]<-paste("000000", xx[62:66], sep="")
xx[67:100]<-paste("00000", xx[67:100], sep="")
......
> nchar(xx)
[1] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
[38] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
[75] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 .... 9
xx<-substring(xx, 6, 9)
This is a solution for one data set would be sufficient but not if I will
continuously deal with this same issue.
Furthermore, I am trying to automate the process but I have not be able to came
with adequate solution.
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.
Another way I thought was to create an if statement, but this will be long and
not efficient (I think).
Any suggestion, will be appreciated.
Jose
[[alternative HTML version deleted]]
Try this:
formatC(c(1, 11, 111, 1111), flag = "0", width = 9)
Or:
sprintf("%09d", c(1, 11, 111))
On Wed, Jan 5, 2011 at 1:50 PM, jose Bartolomei
<surfprjab@hotmail.com>wrote:
>
>
>
> Dear R users,
>
>
> The best in this new year 2011.
>
>
> I am dealing with a character vector (xx) whose nchar are not the same.
>
>
> Ex.
> 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 ....... 9
>
>
> I need xx to be nchar = 9
>
>
> My best guest was to paste “0's”. Then I need substring (xx, 6, 9).
>
>
> I came with:
>
>
> xx[1:61]<-paste("00000000", xx[1:61], sep="")
>
>
> xx[62:66]<-paste("000000", xx[62:66], sep="")
>
>
> xx[67:100]<-paste("00000", xx[67:100], sep="")
>
>
> ......
>
> > nchar(xx)
> [1] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
> 9
> [38] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
> 9 9
> [75] 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 .... 9
>
>
> xx<-substring(xx, 6, 9)
>
>
>
>
> This is a solution for one data set would be sufficient but not if I will
> continuously deal with this same issue.
>
>
> Furthermore, I am trying to automate the process but I have not be able to
> came with adequate solution.
>
>
> 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.
>
>
> Another way I thought was to create an if statement, but this will be long
> and not efficient (I think).
>
>
>
> Any suggestion, will be appreciated.
>
>
> Jose
>
>
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
[[alternative HTML version deleted]]
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.