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]]
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
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 -- View this message in context: r.789695.n4.nabble.com/From-numeric-vector-to-string-vector-tp3303712p3303730.html Sent from the R help mailing list archive at Nabble.com.
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 <bogaso.christofer at gmail.com> 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]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide 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?
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]] > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
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") >> > > ?sprintfJust for my own understanding: Why not as.character(Vect) ? -- Sascha Vieweg, saschaview at gmail.com
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 <saschaview at gmail.com> 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, saschaview at gmail.com > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide 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?