Duhhh... You already knew about nchar!!

So (untested):

nc <- nchar(vec)
mx <- max(nc)
blanks <- rep(" ",mx-nchar+1) ## +1 needed for rep
paddedvec <-substring(paste(vec,blanks,sep=""),1,mx)

You can also do this at one go with  str_pad in the stringr package, found
by searching on "pad strings.'

-- Bert



On Thu, Mar 14, 2013 at 7:56 AM, Bert Gunter <bgun...@gene.com> wrote:

> ?nchar  will tell you how many characters are in each string (mod
> multibyte locales)
>
> and you can use this to extend any that are shorter than the max with
> blanks or whatever.
>
> -- Bert
>
> On Thu, Mar 14, 2013 at 7:42 AM, Christofer Bogaso <
> bogaso.christo...@gmail.com> wrote:
>
>> Hello again,
>>
>> Let say I have following string:
>>
>> Vec <- c("sada", "asdsa", "sa")
>>
>> Now I want to make each element of this vector with equal length.
>> Basically I want following vector:
>>
>> c("sada ", "asdsa", "sa   ")
>>
>> Therefore we can get:
>>
>> > nchar(c("sada ", "asdsa", "sa   "))
>> [1] 5 5 5
>>
>>
>> Is there any possiblity that we can do it programetically? Because I
>> need to handle a really big vector.
>>
>> Thanks for your help.
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
>
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

        [[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.

Reply via email to