On May 7, 2014, at 5:15 AM, Abhinaba Roy <abhinabaro...@gmail.com> wrote:

> Hi R-helpers,
> 
> sumx <- summary(mtcars[,c("mpg","disp")])
>> sumx
>      mpg             disp
> Min.   :10.40   Min.   : 71.1
> 1st Qu.:15.43   1st Qu.:120.8
> Median :19.20   Median :196.3
> Mean   :20.09   Mean   :230.7
> 3rd Qu.:22.80   3rd Qu.:326.0
> Max.   :33.90   Max.   :472.0
> 
> I want a dataframe as
> 
>             mpg    disp
> Min.      10.40   71.1
> 1st Qu. 15.43  120.8
> Median 19.20  196.3
> Mean    20.09  230.7
> 3rd Qu. 22.80  326.0
> Max.      33.90  472.0
> 
> How can it be done in R?
> -- 
> Regards
> Abhinaba Roy


summary(), when applied to multiple columns, as you are doing, returns a 
character table object:

> str(sumx)
 'table' chr [1:6, 1:2] "Min.   :10.40  " "1st Qu.:15.43  " ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:6] "" "" "" "" ...
  ..$ : chr [1:2] "     mpg" "     disp"


Note that the actual table elements contain both character and numeric values 
that have been formatted.

If you use:

> sapply(mtcars[, c("mpg", "disp")], summary)
          mpg  disp
Min.    10.40  71.1
1st Qu. 15.42 120.8
Median  19.20 196.3
Mean    20.09 230.7
3rd Qu. 22.80 326.0
Max.    33.90 472.0

this applies the summary() function to each column separately, returning a 
numeric matrix:

> str(sapply(mtcars[, c("mpg", "disp")], summary))
 num [1:6, 1:2] 10.4 15.4 19.2 20.1 22.8 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:6] "Min." "1st Qu." "Median" "Mean" ...
  ..$ : chr [1:2] "mpg" "disp"


If you actually want a data frame, you can coerce the result:

> as.data.frame(sapply(mtcars[, c("mpg", "disp")], summary))
          mpg  disp
Min.    10.40  71.1
1st Qu. 15.42 120.8
Median  19.20 196.3
Mean    20.09 230.7
3rd Qu. 22.80 326.0
Max.    33.90 472.0


> str(as.data.frame(sapply(mtcars[, c("mpg", "disp")], summary)))
'data.frame':   6 obs. of  2 variables:
 $ mpg : num  10.4 15.4 19.2 20.1 22.8 ...
 $ disp: num  71.1 120.8 196.3 230.7 326 ...


Regards,

Marc Schwartz

______________________________________________
R-help@r-project.org mailing list
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 via email to