problems in converting numeric to character

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

problems in converting numeric to character

刘瑞阳
Hi,
I am having trouble converting numeric to characters in the format I desire. To be more specific, I have a number of numeric as follows:

x<-c(1.0,2.0,2.00,2.1)
I want to convert them to characters so that the out put would be c(“1.0”,”2.0”,”2.00”,”2.1”).

However, I used as.character(x) and the output is:
"1"   "2"   "2"   “2.1"

The decimals are removed if the numeric ends with “.0”. Is there a way to circumvent this problem?

Thanks very much!

Sincerely,

Ruiyang Liu
______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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
|

Re: problems in converting numeric to character

Adrian Dușa
Does this helps?

> formatC(x, digits = 1, format = "f")
[1] "1.0" "2.0" "2.0" "2.1"


On Thu, Jun 7, 2018 at 10:08 PM 刘瑞阳 <[hidden email]> wrote:

> Hi,
> I am having trouble converting numeric to characters in the format I
> desire. To be more specific, I have a number of numeric as follows:
>
> x<-c(1.0,2.0,2.00,2.1)
> I want to convert them to characters so that the out put would be
> c(“1.0”,”2.0”,”2.00”,”2.1”).
>
> However, I used as.character(x) and the output is:
> "1"   "2"   "2"   “2.1"
>
> The decimals are removed if the numeric ends with “.0”. Is there a way to
> circumvent this problem?
>
> Thanks very much!
>
> Sincerely,
>
> Ruiyang Liu
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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
|

Re: problems in converting numeric to character

Jeff Newmiller
In reply to this post by 刘瑞阳
?formatC (digits, drop0trailing)
?sprintf (format %f)
?cat
?options (digits)

You appear to be confusing source code formatting with output formatting. The internal representation of a numeric value has no notion of the number of decimals that were used to enter it into memory from source code. By the time you (or R) decide to re-convert it to a visual representation (characters), all trace of the original representation has been forgotten, so you have to be explicit about your output format, or R will make assumptions.

On June 7, 2018 8:12:27 AM PDT, "刘瑞阳" <[hidden email]> wrote:

>Hi,
>I am having trouble converting numeric to characters in the format I
>desire. To be more specific, I have a number of numeric as follows:
>
>x<-c(1.0,2.0,2.00,2.1)
>I want to convert them to characters so that the out put would be
>c(“1.0”,”2.0”,”2.00”,”2.1”).
>
>However, I used as.character(x) and the output is:
>"1"   "2"   "2"   “2.1"
>
>The decimals are removed if the numeric ends with “.0”. Is there a way
>to circumvent this problem?
>
>Thanks very much!
>
>Sincerely,
>
>Ruiyang Liu
>______________________________________________
>[hidden email] mailing list -- To UNSUBSCRIBE and more, see
>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.

--
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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
|

Re: problems in converting numeric to character

Sigbert Klinke
In reply to this post by 刘瑞阳
sprintf("%.1f", x)
sprintf("%.2f", x)

Am 07.06.2018 um 17:12 schrieb 刘瑞阳:

> Hi,
> I am having trouble converting numeric to characters in the format I desire. To be more specific, I have a number of numeric as follows:
>
> x<-c(1.0,2.0,2.00,2.1)
> I want to convert them to characters so that the out put would be c(“1.0”,”2.0”,”2.00”,”2.1”).
>
> However, I used as.character(x) and the output is:
> "1"   "2"   "2"   “2.1"
>
> The decimals are removed if the numeric ends with “.0”. Is there a way to circumvent this problem?
>
> Thanks very much!
>
> Sincerely,
>
> Ruiyang Liu
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

--
Sprechstunde: Fr 12-13, SPA1, R308
https://hu.berlin/sk
https://hu.berlin/mmstat3


______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.