Quantcast

From numeric vector to string vector

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

From numeric vector to string vector

Bogaso
Hi there, I have a numeric vector let say:

 

Vect <- c(12.234, 234.5675, 1.5)

 

Now I want a string vector like:

 

changedVec <- c("012.234", "234.568", "001.500")

 

Would be grateful if somebody help me how can I do that.

 

Thanks and regards,


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: From numeric vector to string vector

David Winsemius

On Feb 13, 2011, at 10:43 AM, Bogaso Christofer wrote:

> Hi there, I have a numeric vector let say:
>
> Vect <- c(12.234, 234.5675, 1.5)
>
> Now I want a string vector like:
>
> changedVec <- c("012.234", "234.568", "001.500")
>

?sprintf
>




David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: From numeric vector to string vector

Dieter Menne
In reply to this post by Bogaso
Bogaso wrote
Vect <- c(12.234, 234.5675, 1.5)
Now I want a string vector like:
changedVec <- c("012.234", "234.568", "001.500")
sprintf("%06.3f", c(12.234, 234.5675, 1.5))

Dieter

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: From numeric vector to string vector

jholtman
In reply to this post by Bogaso
Is this what you want:

> Vect <- c(12.234, 234.5675, 1.5)
> sprintf("%07.3f", Vect)
[1] "012.234" "234.567" "001.500"
>


On Sun, Feb 13, 2011 at 10:43 AM, Bogaso Christofer
<[hidden email]> wrote:

> Hi there, I have a numeric vector let say:
>
>
>
> Vect <- c(12.234, 234.5675, 1.5)
>
>
>
> Now I want a string vector like:
>
>
>
> changedVec <- c("012.234", "234.568", "001.500")
>
>
>
> Would be grateful if somebody help me how can I do that.
>
>
>
> Thanks and regards,
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: From numeric vector to string vector

Peter Ehlers
In reply to this post by Bogaso
On 2011-02-13 07:43, Bogaso Christofer wrote:

> Hi there, I have a numeric vector let say:
>
>
>
> Vect<- c(12.234, 234.5675, 1.5)
>
>
>
> Now I want a string vector like:
>
>
>
> changedVec<- c("012.234", "234.568", "001.500")

Just for completeness, let's add formatC to your options:

  formatC( Vect, digits=3, width=7, format="f", flag=0 )

Peter Ehlers

>
>
>
> Would be grateful if somebody help me how can I do that.
>
>
>
> Thanks and regards,
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: From numeric vector to string vector

Sascha Vieweg
In reply to this post by David Winsemius
On 11-02-13 10:48, David Winsemius wrote:

> On Feb 13, 2011, at 10:43 AM, Bogaso Christofer wrote:
>
>>  Hi there, I have a numeric vector let say:
>>
>>  Vect <- c(12.234, 234.5675, 1.5)
>>
>>  Now I want a string vector like:
>>
>>  changedVec <- c("012.234", "234.568", "001.500")
>>
>
> ?sprintf

Just for my own understanding: Why not

as.character(Vect)

?


--
Sascha Vieweg, [hidden email]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: From numeric vector to string vector

jholtman
just using as.character does not add the leading/trailing zeros:

> Vect <- c(12.234, 234.5675, 1.5)
> as.character(Vect)
[1] "12.234"   "234.5675" "1.5"
>


On Sun, Feb 13, 2011 at 12:04 PM, Sascha Vieweg <[hidden email]> wrote:

> On 11-02-13 10:48, David Winsemius wrote:
>
>> On Feb 13, 2011, at 10:43 AM, Bogaso Christofer wrote:
>>
>>>  Hi there, I have a numeric vector let say:
>>>
>>>  Vect <- c(12.234, 234.5675, 1.5)
>>>
>>>  Now I want a string vector like:
>>>
>>>  changedVec <- c("012.234", "234.568", "001.500")
>>>
>>
>> ?sprintf
>
> Just for my own understanding: Why not
>
> as.character(Vect)
>
> ?
>
>
> --
> Sascha Vieweg, [hidden email]
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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.
Loading...