1

Whenever I format a table as such:

example <- sample(LETTERS, replace = T)
format(table(example), scientific = T)

The numbers become characters. How can I tell format() my object is numeric without resorting to as.numeric()? I can't find any such parameters in the function's help page. It says that format() objects are usually numeric, so I guess I'm missing some basic command.

My real data looks like this:

> xtabs(...)
      PRU/DF      PSU/ILH      PSU/JFA      PSU/MCL      PSU/SRM      PSU/ULA 
1.040771e+01 0.000000e+00 2.280347e-01 0.000000e+00 0.000000e+00 8.186240e+00 
     PSU/URA      PSU/VGA        PU/AC        PU/AM        PU/AP        PU/BA 
0.000000e+00 1.534169e+01 8.184747e+01 1.410106e+01 1.028717e+01 1.099289e+00 
       PU/GO        PU/MA        PU/MG        PU/MT        PU/PA        PU/PI 
0.000000e+00 4.369910e+01 5.350849e+00 0.000000e+00 4.706721e-01 0.000000e+00

I want to have the console print the numbers prettier so my co-workers don't have a heart attack. This is what I've come up with:

> format(xtabs(...), scientific = F, digits = 1)
   PRU/DF   PSU/ILH   PSU/JFA   PSU/MCL   PSU/SRM   PSU/ULA   PSU/URA   PSU/VGA 
"10.4077" " 0.0000" " 0.2280" " 0.0000" " 0.0000" " 8.1862" " 0.0000" "15.3417" 
    PU/AC     PU/AM     PU/AP     PU/BA     PU/GO     PU/MA     PU/MG     PU/MT 
"81.8475" "14.1011" "10.2872" " 1.0993" " 0.0000" "43.6991" " 5.3508" " 0.0000" 
    PU/PA     PU/PI     PU/RO     PU/RR     PU/TO    PRU/RJ   PSU/CPS   PSU/NRI 
" 0.4707" " 0.0000" "40.6327" "10.3247" " 0.0000" "10.9644" " 0.0000" "55.4122" 

I'd like to get rid of those quotes so the data looks better on the console.

4

4 回答 4

1

Format returns character vectors, so this is in general to be expected. Your problem, I think, results from the fact that the "format" representation of an integer vector c( 1, 2, 3) is "1" "2" "3", whereas the representation of a numeric (real) vector c( 1, 2, 3 ) is "1e+00" "2e+00" "3e+00".

Consider the following:

format( 1:10 )
format( 1:10, scientific= TRUE )
format( as.numeric( 1:10 ), scientific= TRUE )

Therefore, try

format( as.numeric( table( example ) ), scientific= TRUE )

Unfortunately, you cannot omit the as.numeric, since table generates integer values, and you need real.

于 2013-06-25T13:30:15.450 回答
1

The format function returns character vectors with the numbers now "formatted" per your specifications. If you convert the result back to numbers then any formatting is now lost. I think your problem may be rather the difference between creating the formatted output and printing the formatted output. When you create an object, but don't do anything with it, the object is automatically printed using default arguments, one of the defaults is to put quotes around the character strings. If you don't want the quotes then just call print yourself and tell it to not include the quotes:

> example <- sample(LETTERS, replace = T)
> print(format(table(example), scientific = T), quote=FALSE)
example
B E F G H J K L Q S T U W X Z 
1 1 1 2 1 2 3 1 1 1 3 1 1 5 2 

If your main goal is to not use scientific notation then you should look at the zapsmall function which will turn extremely small values (often the culprit in switching to scientific notation) into zeros. Or do options(scipen=5) (or some other value than 5) which will reduce the likelihood of switching to scientific notation in subsequent printing.

于 2013-06-25T16:02:19.883 回答
0

From your comments on @January's answer, it appears you're just looking for c(table(example)).

于 2013-06-25T14:02:22.970 回答
0

Here's a solution I've found:

View(format(xtabs(...), scientific = F, digits = 1))

The table will appear in another window instead of inside the console, like I originally wanted, but it solves my problem of quickly showing pretty data without resorting to long commands. More elegant solutions are welcome!

于 2013-06-25T14:44:31.550 回答