Skip to content
Prev 310296 / 398503 Next

Formatting digits in a table with mix of numbers and characters

Hi,

You can also use formatC().
table[]<-ifelse(!is.na(as.numeric(tab1)),formatC(as.numeric(table),format="fg",width=4),table)
#warnings will be present 
?table
#???? [,1]??? [,2]??? [,3]? 
#[1,] "12.35" "--"??? "? 10"
#[2,] "12.35" "--"??? "NA"? 
#[3,] "--"??? "123.5" "--"? 
A.K.







----- Original Message -----
From: David Winsemius <dwinsemius at comcast.net>
To: Brian Hobbs <brhobbs at gmail.com>
Cc: r-help at r-project.org
Sent: Thursday, November 8, 2012 12:40 PM
Subject: Re: [R] Formatting digits in a table with mix of numbers and characters
On Nov 8, 2012, at 8:14 AM, Brian Hobbs wrote:

            
Appears from your desired output that your interpretation of "significant digits differs from that of the format function's implementation. (See below)
I cannot think of any at the moment.
Not sure.
Warning messages:
1: In ifelse(!is.na(as.numeric(table)), format(as.numeric(table), digits = 4),? :
? NAs introduced by coercion
2: In format(as.numeric(table), digits = 4) : NAs introduced by coercion
? ?  [,1]? ?  [,2]? ?  [,3]? ? 
[1,] " 12.35" "--"? ?  " 10.00"
[2,] " 12.35" "--"? ?  "NA"? ? 
[3,] "--"? ?  "123.46" "--"? ?