[R] problems in converting numeric to character

Sigbert Klinke @|gbert @end|ng |rom w|w|@hu-ber||n@de
Fri Jun 8 09:08:46 CEST 2018


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
> ______________________________________________
> R-help using r-project.org 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



More information about the R-help mailing list