Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Format data frame summary

Tags:

r

Summary: control output format for summary(dataFrame) calls.

Given data frame dataFrame, summary(dataFrame) creates an output which is easy to read only when the number of columns in dataFrame is small.

Question:

How to produce an output that contains 1 fact at a time?

For example:

dataFrame  <- data.frame(x=rnorm(100), y=rnorm(100), z=rnorm(100));
summary(dataFrame);

gives:

       x                  y                  z          
 Min.   :-2.13427   Min.   :-2.12077   Min.   :-2.4453  
 1st Qu.:-0.53324   1st Qu.:-0.78915   1st Qu.:-0.4100  
 Median : 0.05675   Median :-0.22321   Median : 0.1356  
 Mean   : 0.09102   Mean   :-0.04939   Mean   : 0.1306  
 3rd Qu.: 0.69685   3rd Qu.: 0.58358   3rd Qu.: 0.7847  
 Max.   : 2.48204   Max.   : 2.65971   Max.   : 2.0504  

and the desired output would be:

      x          
Min.   :-2.13427 
1st Qu.:-0.53324 
Median : 0.05675 
Mean   : 0.09102 
3rd Qu.: 0.69685 
Max.   : 2.48204 

      y
Min.   :-2.13427 
1st Qu.:-0.53324 
Median : 0.05675 
Mean   : 0.09102 
3rd Qu.: 0.69685 
Max.   : 2.48204 

      z
Min.   :-2.13427 
1st Qu.:-0.53324 
Median : 0.05675 
Mean   : 0.09102 
3rd Qu.: 0.69685 
Max.   : 2.48204 
like image 647
Theo Avatar asked Feb 09 '13 20:02

Theo


People also ask

How do I get a summary in R?

To get the summary of a data frame in R, use the summary() function. To create a data frame in R, use data. frame() function.

How do I get the summary of a column in R?

Descriptive statistics in R (Method 1): summary statistic is computed using summary() function in R. summary() function is automatically applied to each column. The format of the result depends on the data type of the column. If the column is a numeric variable, mean, median, min, max and quartiles are returned.

How do I create a summary table of data in R?

The easiest way to create summary tables in R is to use the describe() and describeBy() functions from the psych library.


1 Answers

You can use function lapply() to apply function summary() to each column and then cbind() to show data as column.

 lapply(dataFrame,function(x) cbind(summary(x)))
$x
            [,1]
Min.    -2.09200
1st Qu. -0.56240
Median   0.07069
Mean    -0.04267
3rd Qu.  0.54060
Max.     2.07800

$y
            [,1]
Min.    -2.55200
1st Qu. -0.62190
Median  -0.07336
Mean    -0.06966
3rd Qu.  0.58540
Max.     2.07100

$z
            [,1]
Min.    -2.80800
1st Qu. -0.52890
Median  -0.03279
Mean    -0.06263
3rd Qu.  0.53730
Max.     2.88500
like image 188
Didzis Elferts Avatar answered Nov 03 '22 16:11

Didzis Elferts